javascript 中的数组文字表示法是什么?什么时候应该使用它?

发布于 2024-07-26 03:58:28 字数 243 浏览 5 评论 0原文

JSLint 给我这个错误:

第 11 行第 33 行的问题:使用数组文字表示法 []。

var myArray = new Array();

什么是数组文字表示法以及为什么它希望我使用它?

它在这里显示 new Array(); 应该可以正常工作......我缺少什么吗?

JSLint is giving me this error:

Problem at line 11 character 33: Use the array literal notation [].

var myArray = new Array();

What is array literal notation and why does it want me to use it instead?

It shows here that new Array(); should work fine... is there something I'm missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

卸妝后依然美 2024-08-02 03:58:28

数组文字表示法是仅使用空括号定义新数组的地方。 在您的示例中:

var myArray = [];

这是定义数组的“新”方式,我认为它更短/更干净。

下面的例子解释了它们之间的区别:

var a = [],            // these are the same
    b = new Array(),   // a and b are arrays with length 0

    c = ['foo', 'bar'],           // these are the same
    d = new Array('foo', 'bar'),  // c and d are arrays with 2 strings

    // these are different:
    e = [3],             // e.length == 1, e[0] == 3
    f = new Array(3);   // f.length == 3, f[0] == undefined

参考声明 JavaScript 时“Array()”和“[]”有什么区别数组?

array literal notation is where you define a new array using just empty brackets. In your example:

var myArray = [];

It is the "new" way of defining arrays, and I suppose it is shorter/cleaner.

The examples below explain the difference between them:

var a = [],            // these are the same
    b = new Array(),   // a and b are arrays with length 0

    c = ['foo', 'bar'],           // these are the same
    d = new Array('foo', 'bar'),  // c and d are arrays with 2 strings

    // these are different:
    e = [3],             // e.length == 1, e[0] == 3
    f = new Array(3);   // f.length == 3, f[0] == undefined

Reference: What’s the difference between “Array()” and “[]” while declaring a JavaScript array?

執念 2024-08-02 03:58:28

另请参阅: var x = new Array(); 有什么问题

除了 Crockford 的论点之外,我认为这也是因为其他语言具有类似的数据结构,并且恰好使用相同的语法; 例如,Python 有列表和字典; 请看下面的例子:

// this is a Python list
a = [66.25, 333, 333, 1, 1234.5]

// this is a Python dictionary
tel = {'jack': 4098, 'sape': 4139}

Python 在语法上也是正确的 Javascript,这不是很巧妙吗? (是的,结尾的分号不见了,但 Javascript 也不需要这些)

因此,通过重用编程中的常见范例,我们使每个人都不必重新学习一些不应该重新学习的东西。

See also: What’s wrong with var x = new Array();

Aside from the Crockford argument, I believe it is also due to the fact that other languages have similar data structures that happen to use the same syntax; for example, Python has lists and dictionaries; see the following examples:

// this is a Python list
a = [66.25, 333, 333, 1, 1234.5]

// this is a Python dictionary
tel = {'jack': 4098, 'sape': 4139}

Isn't it neat how Python is also grammatically correct Javascript? (yes, the ending semi-colons are missing, but those aren't required for Javascript, either)

Thus, by reusing common paradigms in programming, we save everyone from having to relearn something that shouldn't have to.

〗斷ホ乔殘χμё〖 2024-08-02 03:58:28

除了 Crockford 的争论之外,jsPerf 还表示它更快。 http://jsperf.com/new-vs-literal-array-declaration

Aside from the Crockford argument, jsPerf says that it's faster. http://jsperf.com/new-vs-literal-array-declaration

冰之心 2024-08-02 03:58:28

在查看了@ecMode jsperf之后,我做了一些进一步的测试。

当使用push添加到数组时,new Array()在Chrome上要快得多:

http ://jsperf.com/new-vs-literal-array-declaration/2

使用索引添加对于[]来说稍微快一些。

After looking at @ecMode jsperf, I did some further tests.

When using push to add to the array new Array() is considerably faster on Chrome:

http://jsperf.com/new-vs-literal-array-declaration/2

Using index to add is slightly faster for [].

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文