Created
April 14, 2014 18:00
-
-
Save cesarpachon/10669760 to your computer and use it in GitHub Desktop.
test the concept of chaining prototypes in javascript
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
var A = function(){ | |
console.log("constructor A"); | |
//create an attribute | |
this.a0 = "a0"; | |
} | |
//adding another attribute | |
A.prototype.a1 = "a1"; | |
var B = function(){ | |
console.log("constructor B"); | |
//invoke parent constructor to create a0 here | |
A.call(this); | |
//create a own attribute | |
this.b0 = "b0"; | |
} | |
//chaining the prototypes | |
B.prototype = new A(); | |
//adding another attribute | |
B.prototype.b1 = "b1"; | |
console.log("creating object a"); | |
var a = new A(); | |
console.log(a); | |
console.log(a.a0); | |
console.log(a.a1); | |
console.log("creating object b"); | |
var b = new B(); | |
console.log(b); | |
console.log(b.a0); | |
console.log(b.a1); | |
console.log(b.b0); | |
console.log(b.b1); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment