JS-101-Fundamentals
  • JavaScript Library
  • 1-JavaScript Fundamentals
    • 0-Getting Started
      • Welcome
    • 1-JS-Fundamentals
      • 1-Grammar and Types
        • Comments
        • Declarations
        • Scope
        • Hoisting
        • Types
        • Literals
      • 2-Control Flow and Error Handling
        • if
        • if else
        • switch
        • try catch
        • throw
      • 3-Loops
        • For Loops
        • For In Loops
        • For Of Loops
        • Do While
        • While Loops
      • 4-Functions
        • Declarations
        • Expressions
        • Calling Functions
        • Scope
        • Parameters
        • Arguments
        • Closures
        • Closures Challenge
        • Arrow Functions
        • Predefined
      • 5-Expressions and Operators
        • Assignment
        • Comparison
        • Ternary
        • Typeof
        • Left Hand Side
        • Spread
      • 6 Numbers and Dates
        • Numbers
          • Numbers Enhanced
        • Math
        • Dates
      • 7 String Methods
        • String Literals
        • Methods
      • 8 Regular Expressions
        • Basic Intro
      • 9 Arrays
        • Array Review
        • Populating/Referring
        • Length
        • Iterating
        • Methods
      • 10 Objects
        • About Objects
        • Properties
        • Enumeration
        • Initializers
        • Constructor Functions
        • this
        • create
        • Methods
      • 11 ES6 Intro
        • ES6 Intro
        • let
        • const
Powered by GitBook
On this page
  • File Location
  • Making a method
  • Practice
  • Bonus challenge
  1. 1-JavaScript Fundamentals
  2. 1-JS-Fundamentals
  3. 10 Objects

Methods

File Location

We will be working in the following file:

    javascript-library
        └── 0-PreWork
        └── 1-Fundamentals
             └── 11-Objects
                07-methods.js <----You will be working in this file.

You can create your own methods as properties of an object. Simply name the method, then provide its function. These methods are called in the same way that other properties are called.

Making a method

One of the previous models had a bonus challenge for taking a word and capitalizing the first letter, then printing the result to the console. Let's make that a method of an object.

let obj = {
    //the given input becomes name
    capitalize: function(name) {
        let newName = ""; //newName is a blank string where the letters from name will be entered
        for (let i = 0; i < name.length; i++) {
            if (i == 0) {
                newName += name[0].toUpperCase(); //the first letter of name is capitalized, then added to the string newName
            } else {
                newName += name[i].toLowerCase(); //each subsequent letter is made lowercase, then added the string newName
            }
        }
        console.log(newName);
    } 
}

We call the method by obj.capitalize('aaron'), with the result being 'aaron'.

Practice

  1. Create an object with a method that would multiply an input by 2, then print the result to the console.

  2. Add another method to the object performs some function. Use comments to explain what the method does and show an example.

Bonus challenge

Make an object with a method that creates an array of numbers from 1 to a given input, then print the numbers to the console in both ascending and descending order.

PreviouscreateNext11 ES6 Intro

Last updated 7 years ago