Skip to content

useArrayLiterals

诊断类别:lint/correctness/useArrayLiterals

¥Diagnostic Category: lint/correctness/useArrayLiterals

自从:v1.7.2

¥Since: v1.7.2

来源:

¥Sources:

禁止数组构造函数。

¥Disallow Array constructors.

通常不鼓励使用 Array 构造函数来构造新数组,而应使用数组文字表示法,因为存在单参数陷阱,并且 Array 全局变量可能会被重新定义。例外情况是,当 Array 构造函数通过为构造函数提供单个数字参数有意创建指定大小的稀疏数组时。

¥Use of the Array constructor to construct a new array is generally discouraged in favor of array literal notation because of the single-argument pitfall and because the Array global may be redefined. The exception is when the Array constructor intentionally creates sparse arrays of a specified size by giving the constructor a single numeric argument.

¥Examples

¥Invalid

Array();
code-block.js:1:1 lint/correctness/useArrayLiterals ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Don’t use Array constructors.

> 1 │ Array();
^^^^^^^^
2 │

Use of the Array constructor is not allowed except creating sparse arrays of a specified size by giving a single numeric argument.

The array literal notation [] is preferable.

Array(0, 1, 2);
code-block.js:1:1 lint/correctness/useArrayLiterals ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Don’t use Array constructors.

> 1 │ Array(0, 1, 2);
^^^^^^^^^^^^^^^
2 │

Use of the Array constructor is not allowed except creating sparse arrays of a specified size by giving a single numeric argument.

The array literal notation [] is preferable.

new Array(0, 1, 2);
code-block.js:1:1 lint/correctness/useArrayLiterals ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Don’t use Array constructors.

> 1 │ new Array(0, 1, 2);
^^^^^^^^^^^^^^^^^^^
2 │

Use of the Array constructor is not allowed except creating sparse arrays of a specified size by giving a single numeric argument.

The array literal notation [] is preferable.

Array(...args);
code-block.js:1:1 lint/correctness/useArrayLiterals ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Don’t use Array constructors.

> 1 │ Array(…args);
^^^^^^^^^^^^^^^
2 │

Use of the Array constructor is not allowed except creating sparse arrays of a specified size by giving a single numeric argument.

The array literal notation [] is preferable.

¥Valid

Array(500);
[0, 1, 2];

¥Related links