JavaScript数组声明:新的Array(),新的Array(3),['一','B','C']创建不同行为的阵列 [英] Javascript array declaration: new Array(), new Array(3), ['a', 'b', 'c'] create arrays that behave differently

查看:205
本文介绍了JavaScript数组声明:新的Array(),新的Array(3),['一','B','C']创建不同行为的阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑这个例子的Javascript code:

Consider this example Javascript code:

a = new Array();
a['a1']='foo';
a['a2']='bar';

b = new Array(2);
b['b1']='foo';
b['b2']='bar';

c=['c1','c2','c3'];

console.log(a);
console.log(b);
console.log(c);

是结果Firebug控制台如下:

Results in the Firebug console are as follows:

对于一个(在'[]'必须通过点击+按钮进行扩展):

For a (the '[]' had to be expanded by clicking on the '+' button):

[]      
a1  "foo"   
a2  "bar"

有关A:

[undefined, undefined]

有关C:

["c1", "c2", "c3"]

我的问题是:


  1. 我使用数组['关键'] ='值'的语法是否正确?

  2. 为什么不是数组b如预期?工作

  3. 为什么数组a和c控制台中显示不同?这也似乎jQuery是无法通过数组迭代与它的。每次()方法。

  4. 您能reccomend JavaScript的阵列行为有任何好的教程?

注意:谷歌Chrome的萤火虫只显示[]数组'A',没有选择将其展开。

NOTE: Google Chrome's Firebug displays only [] for array 'a', without the option to expand it.

编辑:好吧,似乎在JavaScript数组只有数字键,因此添加一个字符串作为一个键名使对象进行数组。但是,为什么不jQuery的。每个工作呢?

Alright, it seems that arrays in Javascript have only numerical keys, so adding a string as a key name makes an object out of an array. But why doesn't jQuery's .each work with it?

$.each(a, function ()
    {
    alert ('derp');
    })

这code,附加到脚本,不产生任何警报。

This code, appended to the script, produces no alerts.

推荐答案

数组有数字索引。因此,

Arrays have numerical indexes. So,

a = new Array();
a['a1']='foo';
a['a2']='bar';

and

b = new Array(2);
b['b1']='foo';
b['b2']='bar';

不添加元素到数组,但加入 .a1 .a2 属性设置为 A 对象(数组是对象太)。作为进一步的证据,如果你这样做:

are not adding elements to the array, but adding .a1 and .a2 properties to the a object (arrays are objects too). As further evidence, if you did this:

a = new Array();
a['a1']='foo';
a['a2']='bar';
console.log(a.length);   // outputs zero because there are no items in the array

您第三个选项:

c=['c1','c2','c3'];

被赋予变量 C 具有三个元素的数组。这三个元素可以通过访问: C [0] C [1] C [2] 。换句话说, C [0] ==='C1' c.length === 3

is assigning the variable c an array with three elements. Those three elements can be accessed as: c[0], c[1] and c[2]. In other words, c[0] === 'c1' and c.length === 3.

JavaScript不使用它的阵列功能什么其他语言调用关联数组,你可以使用任何类型的键的数组中为止。您可以通过只在JavaScript中使用对象实现大多数的关联数组的功能,其中每个项目就是这样的一个属性。

Javascript does not use its array functionality for what other languages call associative arrays where you can use any type of key in the array. You can implement most of the functionality of an associative array by just using an object in javascript where each item is just a property like this.

a = {};
a['a1']='foo';
a['a2']='bar';

所以一般使用数组用于此目的,因为它只是混淆人读你的code,并导致错误的假设有关错误如何code ++工程。

It is generally a mistake to use an array for this purpose as it just confuses people reading your code and leads to false assumptions about how the code works.

这篇关于JavaScript数组声明:新的Array(),新的Array(3),['一','B','C']创建不同行为的阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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