为什么在新对象上未定义JavaScript原型属性? [英] Why is JavaScript prototype property undefined on new objects?
问题描述
我对JavaScript原型概念的概念很新。
I'm fairly new to the concept of JavaScript's prototype concept.
考虑以下代码:
var x = function func(){
}
x.prototype.log = function() {
console.log("1");
}
var b = new x();
据我所知, b.log()
应该返回1,因为 x
是它的原型。但为什么属性 b.prototype
未定义?
As I understand it, b.log()
should return 1 since x
is its prototype. But why is the property b.prototype
undefined?
不是 b.prototype
应该返回对 x
函数的引用?
Isn't b.prototype
supposed to return the reference to the x
function?
推荐答案
只有构造函数具有原型。由于 x
是一个构造函数, x
有一个原型。
Only constructor functions have prototypes. Since x
is a constructor function, x
has a prototype.
b
不是构造函数。因此,它没有原型。
b
is not a constructor function. Hence, it does not have a prototype.
如果你想获得对构造 b
的函数的引用(在这种情况下, x
),您可以使用
If you want to get a reference to the function that constructed b
(in this case, x
), you can use
b.constructor
这篇关于为什么在新对象上未定义JavaScript原型属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!