Javascript 数组是原语吗?字符串?对象? [英] Are Javascript arrays primitives? Strings? Objects?

查看:28
本文介绍了Javascript 数组是原语吗?字符串?对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

数组只是伪装的对象吗?为什么/为什么不?他们以什么方式(这样/不)?

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 文字、原语和字符串/对象/数组/等,我需要了解什么?

Am I mislead/mistaken/wrong? What do I need to know about JS literals, primitives, and strings/objects/arrays/etc...?

数组/对象只是伪装的字符串吗?为什么/为什么不?他们以什么方式(这样/不)?

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.

  1. 数组在其原型链中有一个额外的对象——即Array.prototype.这个对象包含所谓的 Array 方法,可以在数组实例上调用.(方法列表在这里:http://es5.github.com/#x15.4.4)

  1. 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)

数组有一个 length 属性(它是实时的,因此,它会自动更新)(阅读此处: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屋!

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