简体   繁体   English

此JavaScript声明是数组还是某种对象

[英]Is this javascript declaration an array or some kind of object

Can someone please explain how the following javascript code: 有人可以解释以下javascript代码如何:

var temp = {};

temp[0] = "a"
temp[1] = "b"
temp[2] = "c"

if different from an array like 如果不同于数组

var temp = new Array();

or 要么

var temp = []

I don't really understand if the first example "temp = {}" can be considered an array or is it some kind of object? 我真的不明白第一个示例“ temp = {}”是否可以视为数组还是某种对象?

var temp = {}; is an object with representation like Object {0: "a", 1: "b", 2: "c"} var temp = [] is an array with representation like ["a", "b", "c"] 是一个对象,其表示形式像Object {0: "a", 1: "b", 2: "c"} var temp = []是一个数组,其表示形式像["a", "b", "c"]

while var temp = new Array(); var temp = new Array(); is again the same thing like temp = [] 再次像temp = []

More detailed information here What's the difference between "Array()" and "[]" while declaring a JavaScript array? 此处有更多详细信息,在声明JavaScript数组时“ Array()”和“ []”之间有什么区别?

The first one creates an object: 第一个创建一个对象:

var temp = {};

The second one creates an array: 第二个创建一个数组:

var temp = new Array();

In any case you can access them as they are an array: 无论如何,您都可以访问它们,因为它们是一个数组:

var temp = {};
temp[1]="in object";
console.log(temp[1]);

same as 如同

var temp = []
temp[1]="in array";
console.log(temp[1]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM