Created
January 15, 2020 20:55
-
-
Save arakno/9d97316d3641b3ebfa39bcb320894278 to your computer and use it in GitHub Desktop.
JS object linking through prototype chains
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
var Person = { | |
first: undefined, | |
last: undefined, | |
personMethod: function personMethod() { | |
console.log( this.first, this.last); | |
} | |
}; | |
var Employee = Object.create(Person); | |
Employee.position = undefined; | |
Employee.employeeMethod = function employeeMethod() { | |
console.log(this.position); | |
}; | |
var Manager = Object.create(Employee); | |
Manager.position = 'Manager'; | |
Manager.department = undefined; | |
Manager.managerMethod = function() { | |
console.log(this.department); | |
}; | |
// Using a new object | |
var manager = Object.create(Manager); | |
manager.first = 'Tom'; | |
manager.last = 'Jones'; | |
manager.department = 'Housewares'; | |
manager.employeeMethod(); // Manager | |
manager.personMethod(); // Tom Jones | |
manager.managerMethod(); // Housewares |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment