Created
March 1, 2013 12:31
-
-
Save Sljubura/5064337 to your computer and use it in GitHub Desktop.
Inheritance
This file contains hidden or 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
// 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