在javascript、阵列和物体中有两个数据结构(实际上,阵列是一种特殊用途,但现在并不对此感到关切)。 一个阵列是收集星群;钥匙可能是非毗连的(即不需要0,1,2,3,可能达到0,51,52,99,102)。 你们可以把财产划给一阵列,但这使得对这些财产进行挖掘更加困难。
物体是可与阵列非常相似地检索的专有钥匙。
瞬间阵列的最简单方式是阵列(使用平方字节),而制造物体的最简单方式是物体字面字面(使用曲解包解):
var myArray = []; // creates a new empty array
var myOtherArray = [ "foo", "bar", "baz"]; // creates an array literal:
// myOtherArray[0] === "foo"
// myOtherArray[1] === "bar"
// myOtherArray[2] === "baz"
//
//
// This would be reasonably called a multidimensional array:
//
var myNestedArray = [ [ "foo", "bar", "baz"], [ "one", "two", "three"] ];
// myNestedArray[0] => [ "foo", "bar", "baz"];
// myNestedArray[1] => [ "one", "two", "three"];
// myNestedArray[0][0] === "foo";
// myNestedArray[1][0] === "one";
var myObject = {}; // creates an empty object literal
var myOtherObject = {
one: "foo",
two: "bar",
three: "baz"
};
// myOtherObject.one === "foo"
// myOtherObject["one"] === "foo" (you can access using brackets as well)
// myOtherObject.two === "bar"
// myOtherObject.three === "baz"
//
//
// You can nest the two like this:
var myNestedObject = {
anArray: [ "foo", "bar", "baz" ],
anObject: {
one: "foo",
two: "bar",
three: "baz"
}
}