"this" object

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

    "this" object

    Hello,

    I'm working on a Javascript interpreter; when I execute this code I cannot
    figure out why the inner "c" function returns the input widget as "this"...
    could someone tell ??

    <input type=button onclick="
    function myobject(a) { function c() { return this; } var prop1=a;
    this.youpla = c(); }
    var i = new myobject();
    alert(i.youpla. innerHTML);
    " value="bonjour" />

    the docs tells when calling "c( )" whithout "object." before it should pass
    null as "this" but I get the widget ?

    please help
    Armel



  • Martin Honnen

    #2
    Re: &quot;this&quot ; object



    Armel Asselin wrote:
    [color=blue]
    > Hello,
    >
    > I'm working on a Javascript interpreter; when I execute this code I cannot
    > figure out why the inner "c" function returns the input widget as "this"...
    > could someone tell ??
    >
    > <input type=button onclick="
    > function myobject(a) { function c() { return this; } var prop1=a;
    > this.youpla = c(); }
    > var i = new myobject();
    > alert(i.youpla. innerHTML);
    > " value="bonjour" />
    >
    > the docs tells when calling "c( )" whithout "object." before it should pass
    > null as "this" but I get the widget ?
    >[/color]

    In my view the this object should be the global object which is the
    window object, and it is that with Netscape and with Opera. Only IE6/Win
    makes the this object the containing <form> object, no idea why. I don't
    think it is the <input> widget itself. Why do you think so? Which
    browser have you tried?


    <html>
    <head>
    <title>this object</title>
    </head>
    <body>
    <form name="formName" >
    <input type="button"
    name="buttonNam e"
    onclick="functi on myobject(a) {
    function c() {
    return this;
    }
    var prop1=a;
    this.youpla = c();
    }
    var i = new myobject();
    alert('i.youpla : ' + i.youpla + '\ni.youpla == i: ' +
    (i.youpla == i) + '\ni.youpla == this: ' + (i.youpla == this) +
    '\ni.youpla == window: ' + (i.youpla == window) + '\ni.youpla ==
    document.formNa me: ' + (i.youpla == document.formNa me));"
    value="bonjour" >
    </form>
    </body>
    </html>
    --

    Martin Honnen


    Comment

    • Lasse Reichstein Nielsen

      #3
      Re: &quot;this&quot ; object

      "Armel Asselin" <armel@mobipock et.com> writes:
      [color=blue]
      > I'm working on a Javascript interpreter; when I execute this code I cannot
      > figure out why the inner "c" function returns the input widget as "this"...
      > could someone tell ??[/color]

      Are you sure that is what it does?
      [color=blue]
      > <input type=button onclick="
      > function myobject(a) { function c() { return this; } var prop1=a;
      > this.youpla = c(); }
      > var i = new myobject();
      > alert(i.youpla. innerHTML);
      > " value="bonjour" />
      >
      > the docs tells when calling "c( )" whithout "object." before it should pass
      > null as "this" but I get the widget ?[/color]

      let's trace through the code.

      --- start ---
      function myobject(a) { function c() { return this; }
      var prop1=a;
      this.youpla = c(); }
      --- Declaration of local function myobject ---
      var i = new myobject();
      --- call to myobject with this = new object ---
      function c() { return this; }
      --- declaration of local function c
      var prop1=a;
      --- local variable set to something, soon to be forgotten
      this.youpla = c(); }
      --- call to local function c ---
      return this;
      --- returns the global object, since function not called as method ---
      --- youpla property of new object set to global object ---
      --- local variable i set to new object
      alert(i.youpla. innerHTML);

      this should alert the innerHTML of the global/window object. No such
      thing exists, so it should alert "undefined" . It does for me in IE 6
      and Opera 7.

      /L
      --
      Lasse Reichstein Nielsen - lrn@hotpop.com
      Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
      'Faith without judgement merely degrades the spirit divine.'

      Comment

      • Armel Asselin

        #4
        Re: &quot;this&quot ; object

        > this should alert the innerHTML of the global/window object. No such[color=blue]
        > thing exists, so it should alert "undefined" . It does for me in IE 6
        > and Opera 7.
        >[/color]

        I used IE 5...



        Comment

        • Lasse Reichstein Nielsen

          #5
          Re: &quot;this&quot ; object

          "Armel Asselin" <armel@mobipock et.com> writes:
          [color=blue]
          > I used IE 5...[/color]

          I was testing with just the posted code, so with no form element
          around it. If I add the form, IE 6 also reports the innerHTML of
          the form.

          It is perhaps an artifact of how IE makes the properties of the
          form visible to the HTML event handler. It is still weird, and
          incorrect according to the ECMAScript standard.

          /L
          --
          Lasse Reichstein Nielsen - lrn@hotpop.com
          Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
          'Faith without judgement merely degrades the spirit divine.'

          Comment

          Working...