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

Re: [jslint] using 'this' in strict mode

Expand Messages
  • Phil
    ... Ah! Thanks. Following through the rest of that thread, the solution is simple [for my case anyway]. Just change the function definitions from function
    Message 1 of 6 , Aug 16, 2011
    • 0 Attachment
      --- In jslint_com@yahoogroups.com, "paulcrowder1979" <paul@...> wrote:
      >
      > Crockford explains his reasoning in this post
      > <http://tech.groups.yahoo.com/group/jslint_com/message/1553> .

      Ah! Thanks.

      Following through the rest of that thread, the solution is simple [for my case anyway]. Just change the function definitions from
      function name() {
      stuff with this
      }
      to
      name = function () {
      stuff with this
      };

      plus adding the function names to the list of vars at the top of module (the 'outer' function). No more complaints, no change in functionality.

      [ .. snip .. snip .. ]
    • Marcos Zanona
      Actually if you work with even listeners it is quite easy to understand why `this` isn t accepted in all cases: ### In the following case `this` is accepted
      Message 2 of 6 , Aug 17, 2011
      • 0 Attachment
        Actually if you work with even listeners it is quite easy to understand why `this` isn't accepted in all cases:

        ###
        In the following case `this` is accepted because its it possible to understand that `this` is inherited to the button scope.
        ----
        button.addEventListener('click', function (event) {
        this.innerText = 'Clicked';
        }, false);
        ----

        ###
        In the following example, `this` violates the rule because it isn't really possible to know which scope `this` is referring to:
        ----
        function onClick(event) {
        this.innerText = 'Clicked'; //what's `this` scope here? window?
        event.target.innerText = 'Clicked'; //this becomes a good alternative;
        }

        button.addEventListener('click', onClick, false);
        ----

        This way I could understand really what this rule is all about, makes sense if you think. And as Crockford says, it feels more professional I believe, but that's just my opinion :)
      Your message has been successfully submitted and would be delivered to recipients shortly.