2

EDIT: this issue is related to JSHint, rather than JSLint - changed the tag.

The following gives me a "possible strict violation". I understand why the violation occurs - this is because of the use of this in a function that jslint doesn't believe is a method:

function Widget(name){
   this.name = name;
}

Widget.prototype.doSomething = doSomething;

function doSomething(){
   console.log(this.name + " did something");
}

Although, the following approaches solve the jslint warning, they force me into a code organization that I would rather avoid:

1) declaring the function inline:

Widget.prototype.doSomething = function (){
   console.log(this.name + " did something");
}

2) creating a wrapper function that passes this:

Widget.prototype.doSomething = function (){ return doSomething(this); };

function doSomething(self){
  // ...
}

Is there a way to organize the code to solve the issue other than using the approaches above?

New Dev
  • 48,427
  • 12
  • 87
  • 129

1 Answers1

1

The way to properly address this is in your question. Do #1:

Widget.prototype.doSomething = function (){
   console.log(this.name + " did something");
}

The whole point of linting is that it prevents you from making common mistakes in source code, and the error you're seeing is completely correct: You shouldn't ever see this mentioned in a function that is not declared on an object.

Linting has nothing to do with the way the program behaves when executed. It's not that linting doesn't "believe" that your method will be invoked on an object, it's that your source code contains a pattern known to be problematic. You can work around it all you like, but as long as the pattern is still in your source, you still have a linting problem.

If you want to ignore it, then wrap it in /*ignore jslint start*/ and /*ignore jslint end*/, but the problem will still be there.

user229044
  • 232,980
  • 40
  • 330
  • 338
  • I think embedded `this` is fine; see my answer which does lint at JSLint.com with `"use strict;"` on. Also `/*ignore jslint start*/` isn't supported in Crockford's current version. See [39 upvotes of a comment that that doesn't work at this linked question](http://stackoverflow.com/questions/599859/jslint-control-comments-selective-ignore/13175495#comment22384801_13175495). – ruffin Mar 17 '15 at 00:11
  • Thanks. Actually, lint has a more precise way of handling this: `/*jshint validthis: true */`, but #1 makes the object public properties less obvious in code. I typically organize all the public properties at the top and leave the rest of the "meat" in functions below. – New Dev Mar 17 '15 at 00:16
  • This is the best way to declare functions on prototype. It makes it extremely clear which `this` we are talking about. Separating "the meat" from the declaration makes people scroll-jump around the file and waste time. +1 from me. – Oleg Mar 17 '15 at 00:22
  • So, the answer is "no, and don't even bother"? – New Dev Mar 17 '15 at 00:32
  • @NewDev Yes. The linter is working as intended. Either accept it's advice, or suppress/ignore this error, or don't use the linter. – user229044 Mar 17 '15 at 00:40
  • @meagar, so, wait... your recommendation is to use #1 for code organization, but for completeness-sake, how work that work with multiple nested function calls? Would you pass `this` as a parameter to inner function calls? – New Dev Mar 17 '15 at 02:19