Array.prototype
// [constructor: ƒ, concat: ƒ, copyWithin: ƒ, fill: ƒ, find: ƒ, …]
Array.prototype[0] // undefined
Array.prototype.concat // ƒ concat() { [native code] }
Array.prototype 输出的这个东西怎么看着像数组,但好像又不是数组,谁能解惑下?
thank you ~(#^.^#)
Array.prototype
// [constructor: ƒ, concat: ƒ, copyWithin: ƒ, fill: ƒ, find: ƒ, …]
Array.prototype[0] // undefined
Array.prototype.concat // ƒ concat() { [native code] }
Array.prototype 输出的这个东西怎么看着像数组,但好像又不是数组,谁能解惑下?
thank you ~(#^.^#)
Array.isArray(Array.prototype) // true
// 另外
Array.isArray(Object.prototype) // false
此处应该得一分(嘿嘿)
Array.prototype.length // 0
typeof Array.prototype // object
另外这种情况 还包括函数,例如:
function fn () {}
fn.prop = {
version: "v1.0.0"
}
fn.prop // {version: "v1.0.0"}~~~~
虽然是函数定义的存在,但是依然可以在作为对象特性的本身定义属性
先说结论:Array.prototype肯定是一个对象
理由:
1、类型检测为object
typeof Array.prototype // object
2、可以直接取自身存在的属性值
Boolean(Array.prototype.concat) // true
3、按照常识,对象的prototype属性肯定是个对象(至少不能是个数组)
至于:
Array.prototype[0] // undefined
很简单,一个普通对象(非类数组对象)的obj[0]也是undefined
至于:
Array.prototype
// [constructor: ƒ, concat: ƒ, copyWithin: ƒ, fill: ƒ, find: ƒ, …]
只是打印出来有点奇怪而已
10 回答11.1k 阅读
6 回答3k 阅读
5 回答4.8k 阅读✓ 已解决
4 回答3.1k 阅读✓ 已解决
2 回答2.6k 阅读✓ 已解决
3 回答5.1k 阅读✓ 已解决
3 回答1.8k 阅读✓ 已解决
补充:
不知道哪位过路神仙踩的我,那我就要问问了,你要觉得我说的哪不对,你倒是给我评论说你有何高见呗?暗搓搓地就知道踩是几个意思?
我就一句话,一切以文档为准。
JS 的标准定义在编号为 ECMA-262 的文档里,咱们看现行版本 —— ECMAScript 2021。
有关 Array 的定义:
翻译过来就是:“
Array
是一个特殊对象,会对特定类型的属性名进行特殊处理。”这里已经说明
Array
是个对象,但还没那么明显。那么再看第 22.1.3 小节的描述:
翻译过来已经说明 Array 的
\[\[Prototype\]\]
就是Object.prototype
,原型指向是相同的,也就是 Array 和 Object 二者的类型相同。最后看 MDN 上有关
Array.protype
的陈述:由此得出结论,JS 里
Array
类型是Object
,而Array.prototype
是Array
,所以类型也是Object
。P.S. 最后再说一遍:
Array
是 JS 的里高阶/内置对象(Built-in Class),它的类型(Type)是Object
。没有一个 Type 叫做Array
的玩意儿。不要把其他语言里有关数组的概念往 JS 里套,JS 只提供了一个看着像数组的对象,并把它起名为Array
了。