是JavaScript的数组元素无非Array对象属性的更多? [英] Are JavaScript Array elements nothing more than Array object properties?

查看:147
本文介绍了是JavaScript的数组元素无非Array对象属性的更多?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请,裸陪我过去非常简单的JavaScript!

Please, bare with me past these lines of very simple JavaScript!

我观察到以下内容:

var o = {}; // empty JS object
var a = []; // empty JS array

o.myproperty = "I am property";
a.push("I am array element");

alert(o['myproperty']); // alerts "I am property"
alert(o.myproperty); // alerts "I am property"
alert(a[0]); // alerts "I am array element"
alert(a['0']); // alerts "I am array element"

/* Of course, accessing an array element using dot notation (e.g. a.0) would cause a
   SyntaxError: Unexpected number (in JavaScript variable names cannot begin with numbers.)
*/

还有:

'myproperty' in o // evaluates to true
0 in a // true
'0' in a // true

delete o.myproperty; // true, o is now empty
delete o['myproperty']; // true, o is now empty
delete a[0]; // true, a contains now 1 undefined element
delete a['0']; // true, a contains now 1 undefined element

看来,如果添加元素的数组实际上创建数组对象的编号属性,那是以后的被引用来访问的元素。但有些事情并不对这些工作的的数组元素的属性的,正是因为他们做正常的对象属性(一个例子是上面删除)。所以......

It seems as if adding an element to an array actually creates a numbered property on the array object, that is afterwards used to access the element by reference. But some things do not work on these array element properties exactly as they do on normal object properties (an example is the above deletion). So...

TL; DR
......通过看不见的编号数组对象的属性通过引用访问JavaScript的数组元素?

TL;DR ... Are javascript array elements accessed by reference via unseen numbered array object properties?

修改
似乎如此:<一href=\"http://stackoverflow.com/questions/500504/why-is-using-for-in-with-array-iteration-such-a-bad-idea\">Why是使用&QUOT;对于...在&QUOT;与数组迭代一个坏主意?

推荐答案

pretty多少唯一一个阵列上添加了一个对象。长度属性,一些特定阵列的方法,如 .push 。这真的是,引擎盖下的阵列只是一个对象。真正使得它可作为一个数组是说。长度参数;它可以让你的迭代的在属性的订购的方式与为(i = 0; I&LT; arr.length;我++)循环。在。长度属性更新,当您 .push()新元素到数组或做一些其他的基于阵列的时候具体操作。这真是所有你需要做的对象作为工作的一个索引,排序列表。

Pretty much the only thing an Array adds over an Object is the .length property, and a few array-specific methods like .push. That's really it, under the hood an Array is just an Object. What really makes it usable as an array is said .length parameter; it allows you to iterate the properties in an ordered manner with a for (i = 0; i < arr.length; i++) loop. The .length property is updated when you .push() new elements into the array or when doing some other array-specific manipulations. And that's really all you need to make an object work as an indexed, ordered list.

这篇关于是JavaScript的数组元素无非Array对象属性的更多?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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