Something odd happening to 'this'

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Loldemort

    Something odd happening to 'this'


    I have an object, a, defined in a js file, with functions b, c and d
    thus:

    function a()
    {
    function b() { ... this.sth.dosth ... }
    function c() { ... this.sth.dosthe lse ... }
    function d() { ... c() ... }

    this.b = b;
    this.d = d;
    }

    and b and d are exposed as methods as shown.

    When called externally as e.g. "x=new a(); x.b();" the function b ca
    find 'this.sth' as expected, and Venkman tells me that the value o
    this is the a object. However, when c (which is defined a few line
    after b, definitely still within a) is called from d e.g. by the we
    page going "x=new a(); x.d();", I find that the value of the 'this
    inside c is the window object, just as if c were global code.

    I am getting around it by coding, inside d, "c.apply( this )", but tha
    seems to be an ugly hack. Have I fallen foul of some kind of Javascrip
    gotcha? Is it a bug?


    Cheers,
    Loldemort

    --
    Loldemor
    -----------------------------------------------------------------------
    Loldemort's Profile: http://www.highdots.com/forums/member.php?userid=1
    View this thread: http://www.highdots.com/forums/showthread.php?t=7097

  • Michael Winter

    #2
    Re: Something odd happening to 'this'

    Loldemort wrote:
    [color=blue]
    > function a()
    > {
    > function b() { ... this.sth.dosth ... }
    > function c() { ... this.sth.dosthe lse ... }
    > function d() { ... c() ... }
    >
    > this.b = b;
    > this.d = d;
    > }
    >
    > [...] [W]hen c (which is defined a few lines after b, definitely
    > still within a) is called from d e.g. by the web page going "x=new
    > a(); x.d();", I find that the value of the 'this' inside c is the
    > window object, just as if c were global code.
    >
    > I am getting around it by coding, inside d, "c.apply( this )", but
    > that seems to be an ugly hack. Have I fallen foul of some kind of
    > Javascript gotcha? Is it a bug?[/color]

    No. It's expected behaviour. The this operator value is determined by
    how a method is called, not just the fact that it is a method. Nested
    functions - those which are properties of a function's activation
    object - will always be given a reference to the global object unless
    overridden. Using call or apply is one way to get around this[1], as
    would passing the this value as an argument to c or creating a
    variable which holds the same value:

    function a() {
    var instance = this;

    function c() {
    /* Use instance in place of this. */
    }
    }

    Further restructuring of the code could also tackle the issue.

    Hope that helps,
    Mike


    [1] Beware that IE implemented them rather late. The call and apply
    methods aren't available in JScript versions prior to 5.5
    (typically installed with IE5.5).

    --
    Michael Winter
    Replace ".invalid" with ".uk" to reply by e-mail.

    Comment

    • Loldemort

      #3
      Re: Something odd happening to 'this'


      Yes, that's most useful thank you

      --
      Loldemor
      -----------------------------------------------------------------------
      Loldemort's Profile: http://www.highdots.com/forums/member.php?userid=1
      View this thread: http://www.highdots.com/forums/showthread.php?t=7097

      Comment

      Working...