no-useless-call (Rules) – Eslint 中文开发手册 - Break易站
[
Eslint 中文开发手册
]
no-useless-call (Rules) - Eslint 中文开发手册
函数调用可以由Function.prototype.call()和写入Function.prototype.apply()。但是Function.prototype.call()并且Function.prototype.apply()比正常的函数调用慢。
规则细节
此规则旨在标记使用情况,Function.prototype.call()并且Function.prototype.apply()可以用正常的函数调用来替代。
此规则的错误代码示例:
1 2 3 4 5 6 7 8 9 10 11 | /*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]); |
此规则的正确代码示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | /*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. foo.apply(undefined, args); foo.apply( null , args); obj.foo.apply(obj, args); |
已知限制
此规则静态比较代码以检查是否thisArg更改。所以如果关于代码thisArg是一个动态表达式,这个规则不能正确判断。
此规则的错误代码示例:
1 2 3 | /*eslint no-useless-call: "error"*/ a[i++].foo.call(a[i++], 1, 2, 3); |
此规则的正确代码示例:
1 2 3 | /*eslint no-useless-call: "error"*/ a[++i].foo.call(a[i], 1, 2, 3); |
何时不使用它
如果你不想被通知不需要做.call()和.apply(),你可以放心禁用此规则。
版本
该规则在 ESLint 1.0.0-rc-1 中引入。
资源
Rule sourceDocumentation source
Eslint 中文开发手册
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架