Created
November 6, 2018 07:53
-
-
Save quangkeu95/d81a9cd4f1dbf0abc19491cab7409a7c to your computer and use it in GitHub Desktop.
Prototype-based Inheritance Class
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
function Person(name, age) { | |
this.name = name; | |
this.age = age; | |
}; | |
Person.prototype.sayHi = function() { | |
console.log(`${this.name}, ${this.age} say hi`); | |
}; | |
function Employee(name, age, job) { | |
this.name = name; | |
this.age = age; | |
this.job = job; | |
}; | |
Employee.prototype.work = function() { | |
console.log(`${this.name} is ${this.job}`); | |
}; | |
// Setup the inheritance chain | |
Employee.prototype.__proto__ = Person.prototype; | |
let employee = new Employee("Chris", 24, "developer"); | |
employee.sayHi(); // Employee can say hi | |
employee.work(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment