Prototypal Inheritance. Whats wrong with this simple example?
function a (){
this.testing = 'testing';
}
function b (){
}
b.prototype = new a();
co开发者_StackOverflow中文版nsole.log(b.testing);
The console shows undefined, rather than 'testing'. What am I doing wrong?
You haven't made an instance of 'b' yet.
var bInstance = new b();
console.log(bInstance.testing);
In other words, the properties of the prototype only appear on objects of type b
, not on the b()
constructor function itself.
精彩评论