您认为每个原型都是
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)。
数组原型对象本身就是一个数组;它的[[Class]]是“Array”,并且它有一个length属性(其初始值为+0)和15.4.5.1中描述的特殊[[DefineOwnProperty]]内部方法。
尝试在 JavaScript 控制台中输入以下内容:
typeof Array.prototype;
Array.prototype
实际上是一个数组。本页对此有详细说明。
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
和 []
。
我不确定这就是原因,但这是我最好的猜测。