什么是JavaScript数组? [英] What is a JavaScript Array?
问题描述
我试图了解JavaScript数组是什么,因为传统的编程语言将数组定义为可以使用偏移量寻址的连续存储区域.
I'm trying to understand what a JavaScript array is because traditional programming languages define an array as a contiguous area of storage that can be addressed using an offset.
现在,普通的JavaScript对象可以寻址为:
Now, a normal JavaScript object can be addressed as:
myObj.myProperty = "my Value";
或
myObj["myProperty"] = "my Value";
因此,JavaScript数组在寻址中只是使用数字而不是名称:
So, a JavaScript array is simply using numbers instead of names in it's addressing:
myObj[0] = "my Value";
myObj.length // === 1
JavaScript数组还具有诸如slice()和join()之类的方法.
A JavaScript Array also has methods, such as slice(), and join().
问:到目前为止我所说的是真的吗?
Q: Is what I said so far true?
推荐答案
JavaScript数组是一个哈希对象,具有使用 Array.prototype
附加的数组函数.简而言之,这是JavaScript中的数组":
A JavaScript array is a hash object with array functions attached using Array.prototype
. Put simply, this is an "Array" in JavaScript:
var x = {
length : 3,
'0' : 'first',
'1' : 'second',
'2' : 'third'
};
x.__proto__ = Array.prototype;
所有数组函数都只作用于索引,正如您所期望的那样,但是您也可以对数组对象执行与对普通JS对象相同的任何操作:
All of the array functions only act on indexes, as you would expect, however you can also do anything to an array object that you would do to a general JS object:
ary.foo = 'bar';
这篇关于什么是JavaScript数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!