我目前正在研究 javascript 原型(prototype)和继承,并提出了以下代码
> Array.constructor === Function;
< true
> Function.constructor === Function;
< true
我不明白为什么 Array.constructor
指向 Function
以及为什么 Function.constructor
指向 Function
?其背后的原因是什么?为什么 javascript 的创建者要让它成为现在的样子?
最佳答案
Array
是一个函数 — 如果不是,您将无法编写 new Array()
。
因此,它的构造函数属性指向Function
。
您可能正在寻找 Array.prototype.constructor
,它确实是 Array
。另一方面,Function.prototype.constructor
是预期的 Function
。
构造函数的 prototype
的属性是由您构造的任何实例继承的;例如 new Array().constructor === Array.prototype.constructor
。
Array.constructor
等静态属性是从 Function.prototype
继承的,因为构造函数是函数,因此是 Function
的实例。
console.log(
Array.prototype.constructor === Array //=> true
)
console.log(
Function.prototype.constructor === Function //=> true
)
关于javascript - 在 javascript 中,为什么 Array.constructor 指向函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43036944/