为什么 Array.prototype 的类型是数组, 而 Object.prototype 是对象

看看下面:

Array.isArray(Array.prototype) //true.

当从chrome浏览器控制台打印出来后

[constructor: ƒ, concat: ƒ, pop: ƒ, push: ƒ, shift: ƒ, …]

但是却不能得到 Array.prototype[0], Array.prototype[1]... or Array.prototype.length

但是Object.prototype 却是

{constructor: ƒ, __defineGetter__: ƒ, __defineSetter__: ƒ,
hasOwnProperty: ƒ, __lookupGetter__: ƒ, …}

为什么啊?

阅读 5.3k
4 个回答
  1. Array.prototype类型为数组可能是Array.prototype上挂的方法都是Array类型适用的,而不能通过Object调用,为了保证这个一致性吧。
  2. 数组也是能添加非数字索引的属性的,至于lenght,你肯定能取到为0。

Array.prototype是一个数组,同时也是一个对象,意味着除了0,1,2。。。等数字索引外还可有有其他的键,它现在是一个空数组,当然没有0,1,2项了

请参考 Array.prototype - JavaScript | MDN 中这段话:

Array.prototype.constructor

所有的数组实例都继承了这个属性,它的值就是 Array,表明了所有的数组都是由 Array 构造出来的。

Array.prototype.length

上面说了,因为 Array.prototype 也是个数组,所以它也有 length 属性,这个值为 0,因为它是个空数组。
新手上路,请多包涵

Array.prototype 打印得出的是它的构造函数 和原型,就是它的实例 空对象
如Number.prototype 是Number { 0 }
String.prototype 是 String { "" }
所有对象都是用.的方式去获取

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题