![](/img/trans.png)
[英]If [] is [] and Array.prototype is [] why doesn't ([] == Array.prototype)
[英]Why Array.prototype is an Array?
您假设每个原型都是Object
是不正确的。
console.log(String.prototype)
console.log(Number.prototype)
console.log(Boolean.prototype)
console.log(Array.prototype)
console.log(Object.prototype)
输出:
String {}
Number {}
Boolean {}
[]
Object {}
来自ECMAScript语言规范 - 15.4.4数组原型对象的属性 (强调我的)
Array原型对象的[[Prototype]]内部属性的值是标准的内置Object原型对象(15.2.4)。
Array原型对象本身就是一个数组 ; 它的[[Class]]是“Array”,它有一个length属性(初始值为+0)和15.4.5.1中描述的特殊[[DefineOwnProperty]]内部方法。
尝试在javascript控制台中输入: typeof Array.prototype;
Array.prototype
实际上是一个Array。 本页详细介绍了这一点。
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/prototype
它可能与事实有关, []
是Array
简写。
所以Array.prototype
指向[]
。 Array.prototype.constructor
指向function Array() { [native code] }
[].constructor
也指向function Array() { [native code] }
所以在猜测时,它是以这种方式完成的,这样你就可以互相使用Array
和[]
。
我不确定这是原因,但这是我最好的猜测。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.