正确的方法在JavaScript中初始化数组的长度是多少? [英] Proper way to initialize an array's length in javascript?

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

问题描述

大多数,我已经在JavaScript中读阵列教程(包括 W3Schools的并的devguru )建议,你可以通过传递给Array构造一个整数初始化具有一定长度的数组 VAR测试=新阵列(4); 语法

在我的js文件从优使用此语法后,我跑了通过 JSLint的的文件之一,它吓坏了:


  

错误:问题1行22字符:预期')',而是看到'4'结果
  VAR测试=新阵列(4);结果
  问题1行23字符:预期';'而是看到')'。结果
  VAR测试=新阵列(4);结果
  1行字符23问题:预期的标识符,而是看到')


通过其行为的的JSLint的解释看完后,它看起来像JSLint的并不真正喜欢新的Array()语法,而是prefers [] 声明数组时。

所以,我有几个问题。首先,为什么呢?我正在运行使用新的Array()语法,而不是什么风险?是否有浏览器兼容问题,我应该知道的?第二,如果我切换到方括号语法,有没有什么办法来声明一个数组,并设置它的长度都在同一行,或做我必须做这样的事情:

  VAR测试= [];
test.length = 4;



  1. 你为什么要初始化的长度是多少?理论上没有必要为此。它甚至可能导致混乱的行为,因为使用所有测试长度来找出一个数组是否为空或不将报告该数组不是空的。结果
    一些 测试显示,设置大型阵列的初始长度的可以的如果阵列事后充满更有效率,但是性能增益(如果有的话)似乎从浏览器到浏览器有所不同。


  2. JSLint的不喜欢新的Array(),因为建筑工是模糊的。

     新阵列(4);

    创建长度的空数组 4。但

     新的Array('4');

    创建一个数组的包含值 4


对于您的评论:在JS你不需要初始化数组的长度。它动态增长。你可以存储在某些变量,例如长度。

  VAR数据= [];
VAR长度= 5; //用户定义的长度对于(VAR I = 0; I<长度;我++){
    data.push(createSomeObject());
}

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.

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

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 ')'.

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.

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. 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.

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

    new Array(4);
    

    creates an empty array of length 4. But

    new Array('4');
    

    creates an array containing the value '4'.

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天全站免登陆