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

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

Expand Messages
  • 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 1 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.