0

I found an interesting line of code in JSHint's config options.

The comment associated with the option reads Tolerate using this in a non-constructor function.

I am confused. Am I misunderstanding the config option?

Aren't there a lot of cases where you want to use this in a non-constructor function? When would you ever want to be warned about it?

Cho Naseo
  • 96
  • 1
  • 8

2 Answers2

1

This options is used to tell JSHint that a function will be invoked with a valid this parameter.

For example:

function myRandomFunction() {
    alert(this.something);
}

myRandomFunction.call(someObject);

If strict mode is on, JSHint will warn that myRandomFunction shouldn't be using this, since it doesn't look like a constructor or a member function.
If you know that it will always be called with a this (eg, as a callback), you can add /*jshint validthis: true */ to suppress the warning.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
-1

I think that every "this" on a non-constructor function could be replaced with a meaningful parameter. On the other hand, badly implemented functions that use the "this" referenced object can easily mess up the application with hard-to-debug errors. So, I really think it's a valid option, specially if project team is large and some members aren't very experienced.

samuelgrigolato
  • 192
  • 1
  • 6