1

I am having a "Possible strict violation" warning from JSHint about the use of this keyword. The OP of this question faced a similar issue (mentioned in the update), but I don't think anyone addressed the reason of it. My code is of the form:

foo.prototype = (function (){
  "use strict";
  function bar (barfoo) {
    if(this.foobar === 0)
    {
      //do something
    }
  }
})();

In this case, JSHint complains about the use of this keyword ("Possible strict violation"). However, the warning goes away if I change it to:

foo.prototype = (function (){
  "use strict";
  var bar = function (barfoo) {
    if(this.foobar === 0)
    {
      //do something
    }
  }
})();

What is the reasoning behind this? I'd like to understand the reason instead of just using a function expression or /* jshint validthis: true */ to suppress it.

Thanks!

Community
  • 1
  • 1
bbtus
  • 97
  • 1
  • 1
  • 10
  • Learn about how `this` works in JS... [You Don't Know JS: `this` Or That?](https://github.com/getify/You-Dont-Know-JS/blob/master/this%20&%20object%20prototypes/ch1.md) – David Gomez Dec 02 '15 at 01:26
  • Thanks for the reference. After reading it, I understand that `this` would be undefined in strict mode if called from the global context, but why would using a function expression instead of a declaration solve the problem? – bbtus Dec 02 '15 at 07:12
  • Is not really solving the problem you are just adding a context. The problem is what you are trying to do (the way you are doing it). If you can put more code and context to your question maybe we can help you to find a solution. – David Gomez Dec 02 '15 at 07:55

0 Answers0