Methods

Object Methods

  • Methods are functions of an object

  • Methods specify the actions of an object (i.e. what the action can do)

  • Methods are called in the same way we call all other functions


Example

const superHero = {
  'secret identity': 'Peter Parker',
  name: 'Spiderman',
  powers: ['super strength', 'hyper awareness', 'agility', 'genius intellect'],
  age: 17,
  sayTagline: function() { // <- method
    console.log("Hey everyone it's your friendly neighborhood Spiderman")
  }
}

// call the the method, just as you would call any function
superHero.sayTagline()

JS Bin on jsbin.com


Exercise

Create add a method to our superVillain object with the key named sayCatchPhrase, the method should print out the following message to the console “I am inevitable”

JS Bin on jsbin.com