Loading ...
Sorry, an error occurred while loading the content.

3024Re: [jslint] `this` in anonymous function without strict violation?

Expand Messages
  • douglascrockford
    Sep 21, 2012
    • 0 Attachment
      --- In jslint_com@yahoogroups.com, "Felix E. Klee" <felix.klee@...> wrote:
      > On Fri, Sep 7, 2012 at 1:21 PM, Tom Worster <fsb@...> wrote:
      > > this is just JSLint saving you time in detecting an ES5 strict mode
      > > error
      > >
      > > see http://is.gd/WCXyRm
      > <url:https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Funct
      > ions_and_function_scope/Strict_mode?redirectlocale=en-US&redirectslug=Ja
      > vaScript%2FStrict_mode#.22Securing.22_JavaScript>
      > Thanks for the link. I now read the article, and I understand that
      > JSLint is just warning about a strict mode error. However, I do not
      > understand why "function f()" is treated differently than "var f =
      > function ()". Aren't the two statements equivalent? Or am I missing
      > something?

      There were famous websites that were depending on having functions with -this- bound to the global object, who then put 'use strict' on the code. That code failed in ES5 browsers but passed JSLint.

      The difficulty for JSLint is that strict mode's operation on -this- is dynamic, but JSLint's analysis is static. How how could I reliably distinguish the case where -this- is used properly in a method from where -this- is used improperly to bind the global object.

      My observation was that competent programmers always used function expressions to produce methods, and that incompetent programmers always used the statement form to bind the global object.

      So when you specify strict mode, JSLint expects you to write like a competent programmer. I know that is a burden for some of you, but that's the best I can do.
    • Show all 14 messages in this topic