JavaScript - 数组和类数组对象之间的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29707568/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JavaScript - Difference between Array and Array-like object
提问by Kramer786
I have been coming across the term "Array-Like Object" a lot in JavaScript. What is it? What's the difference between it and a normal array? What's the difference between an array-like object and a normal object ?
我在 JavaScript 中经常遇到术语“类数组对象”。它是什么?它和普通数组有什么区别?类数组对象和普通对象有什么区别?
采纳答案by Paul S.
What is it?
它是什么?
An Objectwhich has a lengthproperty of a non-negative Integer, and usually some indexed properties. For example
一个具有非负Integer长度属性的对象,通常还有一些索引属性。例如
var ao1 = {length: 0}, // like []
ao2 = {0: 'foo', 5: 'bar', length: 6}; // like ["foo", undefined × 4, "bar"]
You can convert Array-like Objectsto their Arraycounterparts using Array.prototype.slice
您可以将阵列状物体,以他们的阵列使用同行Array.prototype.slice
var arr = Array.prototype.slice.call(ao1); // []
Whats the difference between it and a normal array?
它和普通数组有什么区别?
It's not constructed by Array
or with an Array literal[]
, and so (usually) won't inherit from Array.prototype
. The lengthproperty will not usually automatically update either.
它不是由Array 字面量构造的,Array
也不是用Array 字面量构造的[]
,因此(通常)不会从Array.prototype
. 该长度属性通常不会自动更新两种。
ao1 instanceof Array; // false
ao1[0] = 'foo';
ao1.length; // 0, did not update automatically
Whats the difference between an array-like object and a normal object?
类数组对象和普通对象有什么区别?
There is no difference. Even normal Arraysare Objectsin JavaScript
没有区别。即使是正常的数组是对象中的JavaScript
ao1 instanceof Object; // true
[] instanceof Object; // true
回答by Shahar Shokrani
The famous HTMLCollection
(documentation) and the arguments
(documentation) are array-like object that automatically created.
著名的HTMLCollection
( documentation) 和arguments
( documentation) 是自动创建的类数组对象。
Some quick array-like (e.g HTMLCollection
) differences between real array examples:
HTMLCollection
真实数组示例之间的一些快速数组(例如)差异:
var realArray = ['value1', 'value2'];
var arrayLike = document.forms;
Similarities:
相似之处:
The length getter is the same:
长度吸气剂是一样的:
arrayLike.length; // returns 2;
realArray.length; // returns 2; //there are 2 forms in the DOM.
The indexed getter is the same:
索引 getter 是相同的:
arrayLike[0]; // returns an element.
realArray[0]; // returns an element. ('value')
They are both objects
:
他们都是objects
:
typeof arrayLike; // returns "object"
typeof realArray; // returns "object"
Differences:
区别:
In array-like the join()
, concat()
, includes()
etc, methods are not a functions:
在阵列状的join()
,concat()
,includes()
等等,方法是不是一个功能:
arrayLike.join(", "); // returns Uncaught TypeError: arrayLike.join is not a function (also relevant to `concat()`, `includes()` etc.)
realArray.join(", "); // returns "value1, value2"
The array like is not really an array:
数组 like 并不是真正的数组:
Array.isArray(arrayLike); //returns "false"
Array.isArray(realArray); //returns "true"
In array like you can't set the length property:
在像你不能设置长度属性的数组中:
arrayLike.length = 1;
arrayLike.length; //return 2; //there are 2 forms in the DOM.
realArray.length = 1;
realArray.length; //return 1;
回答by Erich Horn
I think, in ES6, something is Array-like if it is iterable (has a [Symbol.iterator]
property).
我认为,在 ES6 中,如果某些东西是可迭代的(具有[Symbol.iterator]
属性),则它类似于 Array 。