javascript - Array和类似Array的对象之间的区别 [英] javascript - Difference between Array and Array-like object

查看:170
本文介绍了javascript - Array和类似Array的对象之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在javascript中经常遇到Array-Like Object这个术语。它是什么 ?它和普通阵列有什么区别?类似数组的对象与普通对象的区别是什么?

I have been coming across the term "Array-Like Object" a lot in javascript. What is it ? Whats the difference between it and a normal array ? Whats the difference between an array-like object and a normal object ?

推荐答案


这是什么?

What is it?

一个 Object ,其长度属性为非负 Integer ,通常是一些索引属性。例如

An Object which has a length property of a non-negative Integer, and usually some indexed properties. For example

var ao1 = {length: 0},                     // like []
    ao2 = {0: 'foo', 5: 'bar', length: 6}; // like ["foo", undefined × 4, "bar"]

你可以转换使用 Array.prototype.slice

var arr = Array.prototype.slice.call(ao1); // []




它和普通数组有什么区别?

Whats the difference between it and a normal array?

它不是由 Array Array literal <构造的/ em> [] ,因此(通常)不会从​​ Array.prototype 继承。 length 属性通常也不会自动更新。

It's not constructed by Array or with an Array literal [], and so (usually) won't inherit from Array.prototype. The length property will not usually automatically update either.

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?

没有区别。即使是正常的 Arrays 也是 JavaScript中的对象

There is no difference. Even normal Arrays are Objects in JavaScript

ao1 instanceof Object; // true
[] instanceof Object; // true

这篇关于javascript - Array和类似Array的对象之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆