简体   繁体   English

为什么jslint不喜欢在函数表达式上使用call()?

[英]Why doesn't jslint like the use of call() on a function expression?

I'm getting this error. 我收到此错误。

Unexpected '.'.

for this line: 对于这一行:

}).call(this);

I checked jslinterrors.com and could not find a reason. 我检查了jslinterrors.com ,找不到原因。

Reproduce the error using: 使用以下方法重现该错误:

(function () {
    "use strict";
    var a = 1;
    a = a + 1;
}).call(this);

Probably because your using an anonymous function which is an anti pattern in this way. 可能是因为您以这种方式使用了一个匿名模式的匿名函数。 The following should work: 以下应该工作:

(function () {}())

I don't see anything wrong with with the code that you supplied. 我看不到您提供的代码有什么问题。 Perhaps there is something not right in the parsing that jslint performs, or maybe there is a real reason for the warning, I can't find one. 也许jslint执行的解析中有不正确的东西,或者有可能是发出警告的真正原因,但我找不到。 With a little dancing you can be off with the warning, by writing it like this. 稍微跳舞,您就可以通过这样写警告来摆脱警告。

/*global console */
(function () {
    "use strict";

    return function () {
        var a = 1;
        a = a + 1;
        console.log(this);
    };
}()).call([]); // used array rather than this so that it is easy to see in console

On jsFiddle jsFiddle上

Functions wrapped in parenthesis are meant to be invoked immediately so you cannot use .call() there. 括在括号中的函数应立即调用,因此您不能在那里使用.call()

Example of function use 功能使用示例

(function () {
    // do something
})();

Instead you could do 相反,你可以做

var foo = function () {
    // do something
};

foo.call(this); // within scope

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM