Skip to content

Instantly share code, notes, and snippets.

@YasirGaji
Created January 10, 2022 11:59
Show Gist options
  • Save YasirGaji/2cced55f954bc31fa5f108d9a84c7f3c to your computer and use it in GitHub Desktop.
Save YasirGaji/2cced55f954bc31fa5f108d9a84c7f3c to your computer and use it in GitHub Desktop.
This script gist shows constructors in Es6 syntax for the "OOP in Javascript" article by Yasir Gaji
// 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