Skip to content

Instantly share code, notes, and snippets.

@Sljubura
Created March 1, 2013 12:31
Show Gist options
  • Save Sljubura/5064337 to your computer and use it in GitHub Desktop.
Save Sljubura/5064337 to your computer and use it in GitHub Desktop.
Inheritance
// Inheritance, rent-a-constructor versus prototype based inheritance.
function Parent() {
this.list = ['work', 'friends'];
}
var parent = new Parent();
// Events inherits from a parent object by referencing properties
function Events() {
Events.prototype = parent; // or new Parent() if instance is not available
}
var event = new Events();
// Board inherits from a parent object by renting constructor
function Board() {
Parent.call(this);
}
var board = new Board();
console.log(parent.hasOwnProperty('list')); // true
console.log(event.hasOwnProperty('list')); // false
console.log(board.hasOwnProperty('list')); // true
board.list.push('wife');
// event.list.push('hobby'); // Throws TypeError
console.log(parent.list); // ["work", "friends"]
console.log(board.list); // ["work", "friends", "wife"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment