function a (){
this.testing = 'testing';
}
function b (){
}
b.prototype = new a();
console.log(b.testing);
控制台显示未定义,而不是“测试”。我做错了什么?
最佳答案
您还没有创建 'b' 的实例。
var bInstance = new b();
console.log(bInstance.testing);
换句话说,原型(prototype)的属性只出现在b
类型的对象上,而不是b()
构造函数本身。
关于javascript - 原型(prototype)继承。这个简单的例子有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5454243/