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

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

Expand Messages
  • paulcrowder1979
    Crockford explains his reasoning in this post . Personally I find this rule unacceptable, and
    Message 1 of 6 , Aug 16, 2011
    • 0 Attachment
      Crockford explains his reasoning in this post
      <http://tech.groups.yahoo.com/group/jslint_com/message/1553> .
      Personally I find this rule unacceptable, and there should at least be
      an option to override it if it must be default JSLint behavior. Good
      code can be written using "this" inside a function. I've yet to hear a
      convincing argument as to why this is bad code, as its the best way to
      access the DOM element that triggered the event without causing a memory
      leak via a circular reference:
      (function () { function myChangeHandler() { var v =
      this.value; }
      document.getElementById("myTextbox").addEventListener("change",
      myChangeHandler);}());
      Many JavaScript libraries such as ExtJS rely on using "this" to pass
      parameters to an event handler. JavaScript 1.8.5 also introduced the
      bind() method
      <https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Fu\
      nction/bind> on the Function object's prototype which allows a copy of
      a function to be created that always uses the specified object as its
      scope. The use of "this" in functions isn't going away; if anything, it
      is going to become more prevalent as new features are added to the
      language. It's unreasonable rules like this that are starting to turn
      me off to JSLint.


      [Non-text portions of this message have been removed]
    • 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 2 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 3 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.