Javascript 为什么 JSHINT 抱怨这是一个严格的违规行为?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7688765/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 03:11:40  来源:igfitidea点击:

Why is JSHINT complaining that this is a strict violation?

javascriptjslintstrictjshint

提问by Cheeso

I think this may be a duplicate of Strict Violation using this keyword and revealing module pattern

我认为这可能是使用此关键字并揭示模块模式严格违规的重复

I have this code:

我有这个代码:

function gotoPage(s){
    if(s<=this.d&&s>0){this.g=s; this.page((s-1)*this.p.size);}
}

function pageChange(event, sorter) {
    var dd = event.currentTarget;
    gotoPage.call(sorter, dd[dd.selectedIndex].value);
}

And JSHINT (JSLINT) is complaining. It says "Strict violation." for the highlighted line:

而 JSHINT (JSLINT) 正在抱怨。上面写着“严重违规”。对于突出显示的行:

enter image description here

在此处输入图片说明

Is my use of Function.call()and then referencing the instance, somehow inappropriate?

我使用Function.call()然后引用实例,不知何故不合适?

Is this considered to be bad style?

这被认为是不好的风格吗?

回答by Domenic

JSHint says "Possible strict violation" because you are using thisinside something that, as far as it can tell, is not a method.

JSHint 说“可能严格违反”,因为您在this内部使用的东西,据它所知,不是一种方法。

In non-strict mode, calling gotoPage(5)would bind thisto the global object (windowin the browser). In strict mode, thiswould be undefined, and you would get in trouble.

在非严格模式下,调用gotoPage(5)将绑定this到全局对象(window在浏览器中)。在严格模式下,this会是undefined,你会遇到麻烦。

Presumably, you mean to call this function with a bound thiscontext, e.g. gotoPage.bind(myObj)(5)or gotoPage.call(myObj, 5). If so, you can ignore JSHint, as you will not generate any errors. But, it is telling you that your code is unclear to anyone reading it, because using thisinside of something that is not obviously a method is quite confusing. It would be better to simply pass the object as a parameter:

据推测,您的意思是使用绑定this上下文调用此函数,例如gotoPage.bind(myObj)(5)or gotoPage.call(myObj, 5)。如果是这样,您可以忽略 JSHint,因为您不会产生任何错误。但是,它告诉您,任何阅读它的人都不清楚您的代码,因为this在明显不是方法的内容中使用它非常令人困惑。最好简单地将对象作为参数传递:

function gotoPage(sorter, s) {
    if (s <= sorter.d && s > 0) {
        sorter.g = s;

        sorter.page((s - 1) * sorter.p.size);
    }
}

function pageChange(event, sorter) {
    var dd = event.currentTarget;
    gotoPage(sorter, dd[dd.selectedIndex].value);
}

回答by amenthes

I've had this message for a function that did not start with a capital letter.

我收到了一个不以大写字母开头的函数的消息。

"use strict";

// ---> strict violation
function something() {
    this.test = "";
}


// ---> just fine (note the capital S in Something)
function Something() {
    this.test = "";
}

回答by asulaiman

If you declare the function as a variable instead of using the standard function declaration, jshint will not flag this as a strict violation. So you may do the following -

如果将函数声明为变量而不是使用标准函数声明,则 jshint 不会将此标记为严格违规。因此,您可以执行以下操作 -

var gotoPage = function (s){
    if(s<=this.d&&s>0){this.g=s; this.page((s-1)*this.p.size);}
};


var pageChange = function (event, sorter) {
    var dd = event.currentTarget;
    gotoPage.call(sorter, dd[dd.selectedIndex].value);
};

回答by Flimm

If you're trying to implement a method, you might want to assign to the prototype instead:

如果您正在尝试实现一个方法,您可能希望改为分配给原型:

ExampleClassName.protytpe.gotoPage = function gotoPage(s){
  // code using this
};

JSHint won't warn when the function is being assigned.

JSHint 不会在分配函数时发出警告。