useSpread
¥Summary
-
规则生效日期:
v2.3.6¥Rule available since:
v2.3.6 -
¥Diagnostic Category:
lint/nursery/useSpread -
此规则包含 unsafe 修复程序。
¥This rule has an unsafe fix.
-
此规则的默认严重级别为 information。
¥The default severity of this rule is information.
-
来源:
¥Sources:
-
与
prefer-spread相同¥Same as
prefer-spread
-
¥How to configure
{ "linter": { "rules": { "nursery": { "useSpread": "error" } } }}¥Description
强制使用扩展运算符而不是 .apply()。
¥Enforce the use of the spread operator over .apply().
apply() 方法用于调用一个函数,该函数使用给定的 this 值和以数组形式提供的参数。你可以使用扩展运算符 ... 来实现相同的结果,这种方式更简洁易读。
¥The apply() method is used to call a function with a given this value and arguments provided as an array.
The spread operator ... can be used to achieve the same result, which is more concise and easier to read.
¥Examples
¥Invalid
foo.apply(null, args);code-block.js:1:1 lint/nursery/useSpread FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ apply() is used to call a function with arguments provided as an array.
> 1 │ foo.apply(null, args);
│ ^^^^^^^^^^^^^^^^^^^^^
2 │
ℹ Unsafe fix: Use the spread operator.
1 │ - foo.apply(null,·args);
1 │ + foo(...args);
2 2 │
foo.apply(null, [1, 2, 3]);code-block.js:1:1 lint/nursery/useSpread FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ apply() is used to call a function with arguments provided as an array.
> 1 │ foo.apply(null, [1, 2, 3]);
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │
ℹ Unsafe fix: Use the spread operator.
1 │ - foo.apply(null,·[1,·2,·3]);
1 │ + foo(...[1,·2,·3]);
2 2 │
foo.apply(undefined, args);code-block.js:1:1 lint/nursery/useSpread FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ apply() is used to call a function with arguments provided as an array.
> 1 │ foo.apply(undefined, args);
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │
ℹ Unsafe fix: Use the spread operator.
1 │ - foo.apply(undefined,·args);
1 │ + foo(...args);
2 2 │
obj.foo.apply(obj, args);code-block.js:1:1 lint/nursery/useSpread FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ apply() is used to call a function with arguments provided as an array.
> 1 │ obj.foo.apply(obj, args);
│ ^^^^^^^^^^^^^^^^^^^^^^^^
2 │
ℹ Unsafe fix: Use the spread operator.
1 │ - obj.foo.apply(obj,·args);
1 │ + obj.foo(...args);
2 2 │
¥Valid
foo(...args);
obj.foo(...args);
foo.apply(obj, [1, 2, 3]);¥Related links
Biome v2.1 中文网 - 粤ICP备13048890号