Thursday, April 13, 2017

JavaScript is evolving into a true Object Oriented Language

New OOP features of JavaScript

Working Sample

class Human {
  constructor(name, gender) {
    this.name = name;
    this.gender = gender;
  }
 
  static getGenderFemale() {
    return "female";
  }
 
  static getGenderMale() {
    return "male";
  }
}

class MaleHuman extends Human {
  constructor(name) {
    super(name, Human.getGenderMale());
  }
}

class FemaleHuman extends Human {
  constructor(name) {
    super(name, Human.getGenderFemale());
  }
}

var jack = new Human("Jack", Human.getGenderMale());
console.log(jack);

var peter = new MaleHuman("Peter");
console.log(peter);

var sarah = new FemaleHuman("Sarah");
console.log(sarah);