Why is JSHINT complaining that this is a strict violation?

JavascriptJslintStrictJshint

Javascript Problem Overview


> I think this may be a duplicate of https://stackoverflow.com/questions/6300937/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:

enter image description here

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

Is this considered to be bad style?

Javascript Solutions


Solution 1 - Javascript

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

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

Presumably, you mean to call this function with a bound this context, 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 this inside of something that is not obviously a method is quite confusing. 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);
}

Solution 2 - Javascript

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

Solution 3 - Javascript

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 -

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

Solution 4 - Javascript

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCheesoView Question on Stackoverflow
Solution 1 - JavascriptDomenicView Answer on Stackoverflow
Solution 2 - JavascriptamenthesView Answer on Stackoverflow
Solution 3 - JavascriptasulaimanView Answer on Stackoverflow
Solution 4 - JavascriptFlimmView Answer on Stackoverflow