no-useless-call

禁止对 .call().apply() 进行不必要的调用

函数调用可以由 Function.prototype.call()Function.prototype.apply() 编写。但是 Function.prototype.call()Function.prototype.apply() 比正常的函数调用要慢。

规则详情

此规则旨在标记 Function.prototype.call()Function.prototype.apply() 的使用情况,可以用正常的函数调用替换。

此规则的错误代码示例:

/*eslint no-useless-call: "error"*/

// These are same as `foo(1, 2, 3);`
foo.call(undefined, 1, 2, 3);
foo.apply(undefined, [1, 2, 3]);
foo.call(null, 1, 2, 3);
foo.apply(null, [1, 2, 3]);

// These are same as `obj.foo(1, 2, 3);`
obj.foo.call(obj, 1, 2, 3);
obj.foo.apply(obj, [1, 2, 3]);

此规则的正确代码示例:

/*eslint no-useless-call: "error"*/

// The `this` binding is different.
foo.call(obj, 1, 2, 3);
foo.apply(obj, [1, 2, 3]);
obj.foo.call(null, 1, 2, 3);
obj.foo.apply(null, [1, 2, 3]);
obj.foo.call(otherObj, 1, 2, 3);
obj.foo.apply(otherObj, [1, 2, 3]);

// The argument list is variadic.
// Those are warned by the `prefer-spread` rule.
foo.apply(undefined, args);
foo.apply(null, args);
obj.foo.apply(obj, args);

已知限制

1g8bihVOl3jZHUGyPMREsSyeH/wCVbLi45ZWB6CAbuJpWUYp+OGegnn4K8J0JyT5OExxUn+j1Aa1PKNzOsPwKFQAAyiFBYbe9ZX0p7N3q/CjYy5XspU96gBqhiyairub9jyx9KJYkDbb6d6BtkuEqWhkaqbsKcYTUDnAyYKb4ENKBfmxnh4WUOHMFH84tULCo1HmNI2ag3b3dj6Te4o529ts70MUCWiwAVf5FyFMRedSnSfRIfyeBpJvw23N7RB/

KIjVZ8jbyN1kWo441FXYIh3AmcQhCDgs/8OlFVGexGn4OCCKsdfcgNWXdLQQbb4u

/*eslint no-useless-call: "error"*/

a[i++].foo.call(a[i++], 1, 2, 3);

AoqXtTV6Q8irP1mRQNh34rATVScSSkZAU175f14ev6145njeerHH1txMZ4OQcdup

/*eslint no-useless-call: "error"*/

a[++i].foo.call(a[i], 1, 2, 3);

何时不使用

VUPoE9Nq6d7iihsKAqbqY7NNYDgG4qFl1dnF4aEqRzq0uPWDnvVbwRH3X+6gHkpRdP2Fy8qFxQ/wh4GubcYfGddUAYzylp6pIdQXqjzpQWLpTsnNM+l3MtuEIMeGL7UfTASgnNBybSev9WSQAOZCIbcgt6z6owuWTAlfmgjeJ55fIBGxaf+ox1h3PlNsqF7A