【发布时间】:2015-01-29 10:34:27
【问题描述】:
为什么instance的原型property会抛出undefined?。
function Test(name){
this.name = name;
}
Test.prototype = {
constructor: Test,
sayHello : function(){
console.log(this.name);
}
}
var z = new Test("Hello");
console.log(z.prototype); //undefined
console.log(zUtils.constructor); // Test
我可以通过z.sayHello( 访问sayHello 方法),那为什么我的instance prototype 返回我未定义而不是Test.prototype?。
【问题讨论】:
-
因为你不能像这样得到对象的原型,所以没有这样的属性,因为原型没有作为属性公开。您可以使用
Object.getPrototypeOf方法获取它。例如:Object.getPrototypeOf(z) === Test.prototype. -
@dfsq 你不应该在 cmets 中回答。
标签: javascript oop