no-array-constructor
NOTE: this rule is part of the
推荐
rule set.Enable full set in
deno.json
:{ "lint": { "rules": { "tags": ["推荐"] } } }
Enable full set using the Deno CLI:
deno lint --rules-tags=推荐
This rule can be explictly included to or excluded from the rules present in the current tag by adding it to the
include
or exclude
array in deno.json
:{ "lint": { "rules": { "include": ["no-array-constructor"], "exclude": ["no-array-constructor"] } } }
强制执行数组构造的常规用法。
数组构造通常通过文字表示法进行,例如 []
或 [1, 2, 3]
。不推荐使用 new Array()
或 new Array(1, 2, 3)
。这样做有两个原因。首先,当提供一个参数时,会定义数组的长度,而多个参数则填充不定大小的数组。当只使用文字表示法创建预填充数组时,可以避免这种混淆。避免使用 Array
构造函数的第二个理由是,Array
全局可能会被重新定义。
这个规则的一个例外是,在创建固定大小的新数组时,例如 new Array(6)
。这是创建固定长度数组的常规方式。
无效:
// 这是 4 个元素,而不是大小为 100 的 3 个元素的数组
const a = new Array(100, 1, 2, 3);
const b = new Array(); // 应使用 [] 替代
有效:
const a = new Array(100);
const b = [];
const c = [1, 2, 3];