Classes

Classes are just syntactical sugar syntax

Example 1:

Look under section Prototype Inheritance to see ES5 version

Equivalent to a constructor function and creating a prototype:

class Person {
    //Constructor function
    constructor(name, rank) {
        this.name = name;
        this.rank = rank;
    }
    //Will be added to prototype 
    sayName(){
        console.log("Hello", this.name); 
    }
    firePerson(person) {
        return false; 
    }
}

var nelson = new Person("nelson", 10); 
var nick = new Person("nick", 5); 

nelson.firePerson(nick); -> false

Example 2:

class Manager extends Person (
    constructor(name, rank){
        super(name, rank); //Equivalent to Person.call(this, name, rank); 
    }

    firePerson(person) {
        return this.rank > person.rank; 
    }
}

var steve = new Manager("Steve", 90); 
steve.firePerson(nelson);

Status Method

class Helper {
    static logTwice(message) {
      console.log(message);     
   }
}

Helper.logTwice('Logged'); //No need to instantiate the class

results matching ""

    No results matching ""