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

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

问题描述

我在JavaScript中使用数组阅读的大部分教程(包括 w3schools devguru )建议你可以用一定长度初始化一个数组使用 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并不真的喜欢新的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()语法来冒任何风险?是否存在我应该注意的浏览器不兼容性?第二,如果我切换到方括号语法,有没有办法声明一个数组并在一行上设置它的长度,或者我必须做这样的事情:

So I have a couple questions. 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天全站免登陆