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

2691Re: [jslint] this question

Expand Messages
  • Nagy Endre
    Nov 21, 2011
    • 0 Attachment
      Yes, i realized that in {a: this} the value of "a" is the "window" value, not what we expect, because at runtime probably the object is not created yet,
      so only a method that is called later could have the correct "this" value.

      I think it would be great if JSLINT could display an error in this situation like:
      var o = {
          a : this;
      };

      Because i think is there a little confusion here what will be the value of "this".



      ________________________________
      From: stauren <liuch.pku@...>
      To: jslint_com@yahoogroups.com
      Sent: Monday, November 21, 2011 9:49 AM
      Subject: Re: [jslint] this question


       
      I don't think this is about block scope or functional sope. This is
      probably because the function is executed when the script is being parsed,
      at which time the outer object is not fully established, so the the keyword
      'this' points to the window object.

      Another funny aspect of javascript.

      ---
      stauren (畅)
      blog : http://stauren.net
      email : stauren@...

      On Sat, Nov 19, 2011 at 12:19 AM, <z_mikowski@...> wrote:

      > **
      >
      >
      > Javascript does not provide block scope, only functional scope. Therefore
      > the 'this' in your code takes the value as provided to the enclosing
      > function. You could use the module pattern to get the results you are
      > seeking, e.g.
      >
      > var o = function (){
      > // private stuff
      > return {
      > p : "a",
      > // rest of object here
      > };
      > };
      >
      >
      > Nagy Endre <forewer2000@...> wrote:
      >
      > >
      > >Hi everyone, maybe it's a little offtopic sorry, but can anyone explain
      > why "this" inside the "o" object is refer to window ?
      > >Why it's not refer to the "o" object if we are inside there? I want to
      > access from the returned object in function "f" the "o.p" property without
      > using "o". Thanks!
      > >
      > >var p = 2,
      > > o = {
      > > p: "a",
      > > f: (function (m) {
      > > return {
      > > l: m.p
      > > };
      > > }(this))
      > > };
      > >
      > >p = o.f.l;
      > >
      > >Endre
      > >
      > >
      > >[Non-text portions of this message have been removed]
      > >
      >
      > [Non-text portions of this message have been removed]
      >
      >
      >

      [Non-text portions of this message have been removed]




      [Non-text portions of this message have been removed]
    • Show all 22 messages in this topic