This script gist shows constructors in Es6 syntax for the "OOP in Javascript" article by Yasir Gaji
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// BLOCK ONE | |
{ | |
class Entity { | |
constructor(name) { | |
this.name = name; | |
} | |
} | |
const human = new Entity('Creature'); | |
console.log(human); | |
} | |
// BLOCK TWO | |
{ | |
class Entity { | |
constructor(name, dod) { | |
this.name = name; | |
this.creationDate = new Date(dod); | |
} | |
getTotalYears() { | |
return new Date().getFullYear() - this.creationDate.getFullYear(); | |
} | |
} | |
const human = new Entity('Creature', 'january 09 2000'); | |
console.log(human.getTotalYears()); | |
console.log(human); | |
} | |
// BLOCK THREE | |
{ | |
class person { | |
constructor(name) { | |
this.name = name; | |
} | |
greets() { | |
return `${this.name} says hi`; | |
} | |
} | |
const Yasir = new person('Yasir'); | |
class Entity { | |
constructor(name, regards) { | |
person.call(this, name); | |
this.regards = regards; | |
} | |
} | |
Entity.prototype = Object.create(person.prototype); | |
const MrSmith = new Entity('Mr.Smith', 'how are you?'); | |
console.log(Yasir); | |
console.log(MrSmith); | |
console.log(MrSmith.greets()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment