Overview

Methods

  • Methods in a Class function very similar to a method of an regular JavasScript object

  • Let’s recap, an method in an object is a property that has a function as a value, for example:

  const superHero = {
     secretIdentity: 'Peter Parker',
     name: 'Spiderman',
     sayTagline: function() { // <- method
       console.log("Hey everyone it's your friendly neighborhood Spiderman")
     }
   }
  
  • If we were to rewrite this object as a Class, it may look something like this:
  class superHero {
    constructor(secretIdentity, name) {
     this._secretIdentity = secretIdentity
     this._name = name
    }

    sayTagline() {
      console.log("Hey everyone it's your friendly neighborhood Spiderman")
    }
  }
  
  • The key difference here is that methods of a class are not separated by commas