Firefox JS vs ECMA - am I missing something?

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

    #1

    Firefox JS vs ECMA - am I missing something?

    Q for the language lawyers!
    Here's the code:

    function f(p)
    {
    alert("f("+p+") with this="+this);
    }

    var a = [f];

    a[0]("index");


    -----------------------------
    Shouldn't a[0]("index") invoke f with this===a, since a is the 'base'
    of the reference a[0]?
    It doesn't, it invokes f with (apparently) this===f.

    whereas
    var b = new Object;
    b.m = f;
    b.m("method");

    invokes f with this===b.

    I seem to be about 3 years late asking a question like this, but I'm
    writing this Javascript compiler...
  • Stevo

    #2
    Re: Firefox JS vs ECMA - am I missing something?

    Spike wrote:
    Q for the language lawyers!
    Here's the code:
    function f(p)
    {
    alert("f("+p+") with this="+this);
    }
    var a = [f];
    a[0]("index");
    >
    -----------------------------
    Shouldn't a[0]("index") invoke f with this===a, since a is the 'base'
    of the reference a[0]?
    No. Why are you expecting it to use the base. In the call a[0]() it's
    a[0] that represents this.
    It doesn't, it invokes f with (apparently) this===f.
    Same thing. a[0]===f
    whereas
    var b = new Object;
    b.m = f;
    b.m("method");
    >
    invokes f with this===b.
    Same thing again. The object you're referencing here is b, so b is this.

    Comment

    • John G Harris

      #3
      Re: Firefox JS vs ECMA - am I missing something?

      On Sun, 2 Mar 2008 at 02:38:53, in comp.lang.javas cript, Spike wrote:
      >Q for the language lawyers!
      >Here's the code:
      >
      >function f(p)
      >{
      alert("f("+p+") with this="+this);
      >}
      >
      >var a = [f];
      >
      >a[0]("index");
      >
      >
      >-----------------------------
      >Shouldn't a[0]("index") invoke f with this===a,
      <snip>


      If you did

      var z = a[0];
      z.("index");

      what do you think the 'this' value will be while the function is
      executing ? Why would it be different when you do

      a[0]("index");

      instead ?

      Remember that the 'this' value is calculated by the execution engine. It
      uses a very simple rule and knows nothing about the complications in
      your source code.

      John
      --
      John Harris

      Comment

      Working...