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()可以用正常的函数调用来替代。

    此规则的错误代码示例:

    /*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.
    foo.apply(undefined, args);
    foo.apply(null, args);
    obj.foo.apply(obj, args);

    已知限制

    此规则静态比较代码以检查是否thisArg更改。所以如果关于代码thisArg是一个动态表达式,这个规则不能正确判断。

    此规则的错误代码示例:

    /*eslint no-useless-call: "error"*/
    
    a[i++].foo.call(a[i++], 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 中文开发手册
    ]
    转载请保留页面地址:https://www.breakyizhan.com/javascript/34486.html

    posted on 2020-07-13 21:58  MrAit  阅读(348)  评论(0编辑  收藏  举报

    导航