是JavaScript数组原语?字符串?对象? [英] Are Javascript arrays primitives? Strings? Objects?
问题描述
是数组只是变相的对象?为什么/为什么不呢?以何种方式(S)是他们(例如/不)?
Are arrays merely objects in disguise? Why/why not? In what way(s) are they (such/not)?
我一直以为阵列和JS对象本质上是一样的,主要是因为访问它们是一样的。
I have always thought of arrays and objects in JS as essentially the same, primarily because accessing them is identical.
var obj = {'I': 'me'};
var arr = new Array();
arr['you'] = 'them';
console.log(obj.I);
console.log(arr.you);
console.log(obj['I']);
console.log(arr['you']);
我是误导/错/错了吗?我需要什么了解JS文字,原语和字符串/对象/阵列的/ etc ...?
Am I mislead/mistaken/wrong? What do I need to know about JS literals, primitives, and strings/objects/arrays/etc...?
是数组/对象只是变相的字符串?为什么/为什么不呢?以何种方式(S)是他们(例如/不)?
Are arrays/objects merely strings in disguise? Why/why not? In what way(s) are they (such/not)?
推荐答案
数组是对象。
然而,不同于常规的对象,数组有一定的特殊功能。
However, unlike regular objects, arrays have certain special features.
-
阵列在它们的原型链额外的对象 - 即
Array.prototype
。这个对象包含所谓的Array方法,可以在阵列实例调用。 (方法列表在这里: http://es5.github.com/#x15.4.4 )
Arrays have an additional object in their prototype chain - namely
Array.prototype
. This object contains so-called Array methods which can be called on array instances. (List of methods is here: http://es5.github.com/#x15.4.4)
阵列有一个长度
属性(这是活的,ERGO,它自动更新)(在这里阅读: http://es5.github.com/#x15.4.5.2 )
Arrays have a length
property (which is live, ergo, it auto-updates) (Read here: http://es5.github.com/#x15.4.5.2)
阵列有关于定义新属性的特殊算法(在这里阅读: HTTP://es5.github。 COM /#x15.4.5.1 )。如果您设置一个新的属性到一个数组和属性的名称是可以强制转换为整数(如刺痛 1
, 2
,3
等),那么特殊的算法应用(它是在规范中定义第123页)
Arrays have a special algorithm regarding defining new properties (Read here: http://es5.github.com/#x15.4.5.1). If you set a new property to an array and that property's name is a sting which can be coerced to an integer number (like '1'
, '2'
, '3'
, etc.) then the special algorithm applies (it is defined on p. 123 in the spec)
除了这些三件事情,数组就像普通的物体。
Other than these 3 things, arrays are just like regular objects.
阅读有关规范数组: http://es5.github.com/#x15.4
这篇关于是JavaScript数组原语?字符串?对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!