简体   繁体   English

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

[英]Why is JSHINT complaining that this is a strict violation?

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. JSHINT(JSLINT)在抱怨。 It says "Strict violation." 它说“严格违反”。 for the highlighted line: 对于突出显示的行:

在此处输入图片说明

Is my use of Function.call() and then referencing the instance, somehow inappropriate? 我使用Function.call()然后引用该实例是否有点不合适?

Is this considered to be bad style? 这被认为是不好的风格吗?

JSHint says "Possible strict violation" because you are using this inside something that, as far as it can tell, is not a method. JSHint表示“可能严格违反”,因为据您所知,您正在内部使用this方法,而这并不是一种方法。

In non-strict mode, calling gotoPage(5) would bind this to the global object ( window in the browser). 在非严格模式时,调用gotoPage(5)将结合this全局对象( window中的浏览器)。 In strict mode, this would be undefined , and you would get in trouble. 在严格模式下, this将是undefined ,您会遇到麻烦。

Presumably, you mean to call this function with a bound this context, eg gotoPage.bind(myObj)(5) or gotoPage.call(myObj, 5) . 大概,您的意思是在绑定this上下文的情况下调用此函数,例如gotoPage.bind(myObj)(5)gotoPage.call(myObj, 5) If so, you can ignore JSHint, as you will not generate any errors. 如果是这样,您可以忽略JSHint,因为不会产生任何错误。 But, it is telling you that your code is unclear to anyone reading it, because using this inside of something that is not obviously a method is quite confusing. 但是,它告诉您,您的代码对于任何阅读它的人来说都不是很清楚,因为在显然不是一种方法的内部使用this代码会造成很大的混乱。 It would be better to simply pass the object as a parameter: 最好简单地将对象作为参数传递:

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);
}

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 = "";
}

If you declare the function as a variable instead of using the standard function declaration, jshint will not flag this as a strict violation. 如果将函数声明为变量而不是使用标准函数声明,则jshint不会将其标记为严格违规。 So you may do the following - 因此,您可以执行以下操作-

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);
};

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不会发出警告。

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

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