Skip to content

Instantly share code, notes, and snippets.

@cesarpachon
Created April 14, 2014 18:00
Show Gist options
  • Save cesarpachon/10669760 to your computer and use it in GitHub Desktop.
Save cesarpachon/10669760 to your computer and use it in GitHub Desktop.
test the concept of chaining prototypes in javascript
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