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

RE: [jslint] this question

Expand Messages
  • Rob Richardson
    Try this: var p = 2, o = { p: a }; o.f = (function (m) { return { l: m.p }; }(o)); p = o.f.l; Or this: var p = 2, o = { p: a }; o.prototype.f =
    Message 1 of 22 , Nov 18, 2011
    • 0 Attachment
      Try this:

      var p = 2,
      o = {
      p: "a"
      };
      o.f = (function (m) {
      return {
      l: m.p
      };
      }(o));
      p = o.f.l;

      Or this:

      var p = 2,
      o = {
      p: "a"
      };
      o.prototype.f = function () {
      return {
      l: this.p
      };
      };
      p = o.f.l;

      Rob


      -----Original Message-----
      From: jslint_com@yahoogroups.com [mailto:jslint_com@yahoogroups.com] On
      Behalf Of Nagy Endre
      Sent: Friday, November 18, 2011 7:13 AM
      To: jslint_com@yahoogroups.com
      Subject: [jslint] this question




      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]
    • Nagy Endre
      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
      Message 2 of 22 , 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]
      Your message has been successfully submitted and would be delivered to recipients shortly.