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

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

问题描述

考虑这个示例 Javascript 代码:

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:

对于a(必须通过单击+"按钮来展开[]"):

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

[]      
a1  "foo"   
a2  "bar"

对于 b:

[undefined, undefined]

对于 c:

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

我的问题是:

  1. 我是否正确使用了 array['key']='value' 语法?
  2. 为什么数组 b 没有按预期工作?
  3. 为什么数组 a 和 c 在控制台中显示不同?jQuery 似乎也无法使用它的 .each() 方法遍历数组 a.
  4. 你能推荐一些关于 Javascript 数组行为的好的教程吗?

注意:Google Chrome 的 Firebug 只显示数组a"的 [],没有扩展它的选项.

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

好吧,Javascript 中的数组似乎只有数字键,因此添加字符串作为键名会使对象脱离数组.但是为什么 jQuery 的 .each 不能使用它?

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');
    })

附加到脚本中的此代码不会产生警报.

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';

为此目的使用数组通常是错误的,因为它只会让阅读您的代码的人感到困惑,并导致对代码如何工作的错误假设.

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 数组声明:new Array(), new Array(3), ['a', 'b', 'c'] 创建行为不同的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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