如何在 JavaScript 中初始化数组的长度? [英] How to initialize an array's length in JavaScript?

查看:27
本文介绍了如何在 JavaScript 中初始化数组的长度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读过的关于 JavaScript 数组的大部分教程(包括 w3schoolsdevguru) 建议您可以通过使用 var test = new Array(4); 语法将整数传递给 Array 构造函数.

Most of the tutorials that I've read on arrays in JavaScript (including w3schools and devguru) suggest that you can initialize an array with a certain length by passing an integer to the Array constructor using the var test = new Array(4); syntax.

在我的 js 文件中大量使用这种语法后,我通过 jsLint 运行了其中一个文件,它吓坏了:

After using this syntax liberally in my js files, I ran one of the files through jsLint, and it freaked out:

错误:第 1 行字符 22 出现问题:应为)",而看到的是4".
var test = new Array(4);
第 1 行字符 23 处的问题:应为 ';'而是看到了)".
var test = new Array(4);
第 1 行字符 23 出现问题:需要一个标识符,但看到的是)".

Error: Problem at line 1 character 22: Expected ')' and instead saw '4'.
var test = new Array(4);
Problem at line 1 character 23: Expected ';' and instead saw ')'.
var test = new Array(4);
Problem at line 1 character 23: Expected an identifier and instead saw ')'.

阅读jsLint对其行为的解释后,看起来jsLint并不真正喜欢new Array() 语法,而在声明数组时更喜欢 [].

After reading through jsLint's explanation of its behavior, it looks like jsLint doesn't really like the new Array() syntax, and instead prefers [] when declaring arrays.

所以我有几个问题:

首先,为什么?我是否会因为使用 new Array() 语法而冒任何风险?是否存在我应该注意的浏览器不兼容问题?

First, why? Am I running any risk by using the new Array() syntax instead? Are there browser incompatibilities that I should be aware of?

第二,如果我切换到方括号语法,有什么方法可以声明一个数组并将其长度全部设置在一行上,或者我必须这样做:

And second, if I switch to the square bracket syntax, is there any way to declare an array and set its length all on one line, or do I have to do something like this:

var test = [];
test.length = 4;

推荐答案

  1. 为什么要初始化长度?理论上没有这个必要.它甚至会导致令人困惑的行为,因为所有使用 length 来确定数组是否为空的测试都会报告该数组不为空.
    一些 测试表明设置大数组的初始长度如果之后填充数组会更有效,但性能增益(如果有)似乎因浏览器而异.

  1. Why do you want to initialize the length? Theoretically there is no need for this. It can even result in confusing behavior, because all tests that use the length to find out whether an array is empty or not will report that the array is not empty.
    Some tests show that setting the initial length of large arrays can be more efficient if the array is filled afterwards, but the performance gain (if any) seem to differ from browser to browser.

jsLint 不喜欢 new Array() 因为构造函数不明确.

jsLint does not like new Array() because the constructer is ambiguous.

new Array(4);

创建一个空数组长度 4.但是

creates an empty array of length 4. But

new Array('4');

创建一个数组包含值 '4'.

关于您的评论:在 JS 中,您不需要初始化数组的长度.它动态增长.您可以将长度存储在某个变量中,例如

Regarding your comment: In JS you don't need to initialize the length of the array. It grows dynamically. You can just store the length in some variable, e.g.

var data = [];
var length = 5; // user defined length

for(var i = 0; i < length; i++) {
    data.push(createSomeObject());
}

这篇关于如何在 JavaScript 中初始化数组的长度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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