Skip to content

Instantly share code, notes, and snippets.

@arakno
Created January 15, 2020 20:55
Show Gist options
  • Save arakno/9d97316d3641b3ebfa39bcb320894278 to your computer and use it in GitHub Desktop.
Save arakno/9d97316d3641b3ebfa39bcb320894278 to your computer and use it in GitHub Desktop.
JS object linking through prototype chains
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