"this" in ctor

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • andrew.bell.ia@gmail.com

    "this" in ctor

    Hi,

    I've got this very simple code that I don't understand (output follows
    code).

    Why does access to the _field variable fail without the "this"? I
    thought that once _field was added to the prototype object (an
    Integer, in this case), it was just like any other property of the
    object (like _val, which is set in the Integer ctor).

    =============== =============== ==

    Integer = function(i)
    {
    this._val = i;
    }

    Foo = function()
    {
    writeln("This Foo value = " + this._val + "!")
    writeln("Foo value = " + _val + "!")
    writeln("This Foo field = " + this._field + "!");
    writeln("Foo field = " + _field + "!");
    }
    Foo.prototype = new Integer(1);
    Foo.prototype._ field = "foo field";

    =============== =============== ======

    js>run("test2.j s");
    This Foo value = 1!
    Foo value = 1!
    This Foo field = foo field!
    test2.js:11 ReferenceError: _field is not defined
  • Henry

    #2
    Re: "this&quot ; in ctor

    On Sep 9, 2:36 pm, andrew.bell...@ gmail.com wrote:
    <snip>
    Why does access to the _field variable fail without the "this"? I
    thought that once _field was added to the prototype object (an
    Integer, in this case), it was just like any other property of the
    object (like _val, which is set in the Integer ctor).
    <snip>
    writeln("Foo field = " + _field + "!");}
    <snip>
    test2.js:11 ReferenceError: _field is not defined
    Unqualified Identifiers such as - _field - are resolved against the
    scope chain not the prototype chain. There is no - _field - on the
    scope chain so you get an error when attempting to read its value in
    that way.

    Comment

    • andrew.bell.ia@gmail.com

      #3
      Re: &quot;this&quot ; in ctor

      On Sep 9, 8:46 am, Henry <rcornf...@rain drop.co.ukwrote :
      On Sep 9, 2:36 pm, andrew.bell...@ gmail.com wrote:
      <snip>
      >
      Why does access to the _field variable fail without the "this"?  I
      thought that once _field was added to the prototype object (an
      Integer, in this case), it was just like any other property of the
      object (like _val, which is set in the Integer ctor).
      <snip>
        writeln("Foo field = " + _field + "!");}
      <snip>
      test2.js:11     ReferenceError: _field is not defined
      >
      Unqualified Identifiers such as - _field - are resolved against the
      scope chain not the prototype chain. There is no - _field - on the
      scope chain so you get an error when attempting to read its value in
      that way.
      I get that, but then why does the access to _val succeed?

      Thanks,

      Comment

      • Gregor Kofler

        #4
        Re: &quot;this&quot ; in ctor

        andrew.bell.ia@ gmail.com meinte:
        I get that, but then why does the access to _val succeed?
        I suppose because there is a _val defined somewhere else. Running your
        example in the Firebug console gives the expected "ReferenceError :_val
        is not defined".

        BTW: I'm sure you didn't post the complete code. At least a "Foo()" is
        missing.

        Gregor


        --
        http://photo.gregorkofler.at ::: Landschafts- und Reisefotografie
        http://web.gregorkofler.com ::: meine JS-Spielwiese
        http://www.image2d.com ::: Bildagentur für den alpinen Raum

        Comment

        • Henry

          #5
          Re: &quot;this&quot ; in ctor

          On Sep 9, 3:00 pm, andrew.bell...@ gmail.com wrote:
          On Sep 9, 8:46 am, Henry wrote:
          >On Sep 9, 2:36 pm, andrew.bell...@ gmail.com wrote:
          ><snip>
          >
          >>Why does access to the _field variable fail without the
          >>"this"? I thought that once _field was added to the
          >>prototype object (an Integer, in this case), it was
          >>just like any other property of the object (like _val,
          >>which is set in the Integer ctor).
          >><snip>
          >> writeln("Foo field = " + _field + "!");}
          >><snip>
          >>test2.js:11 ReferenceError: _field is not defined
          >
          >Unqualified Identifiers such as - _field - are resolved
          >against the scope chain not the prototype chain. There
          >is no - _field - on the scope chain so you get an error
          >when attempting to read its value in that way.
          >
          I get that, but then why does the access to _val succeed?
          The code you have posed is not representative of the issue (it cannot
          even produce the error you have reported as it does not include any
          calls to - Foo -) and you will not get that question answered until
          you provide code and context that can be used to reproduce the issue.

          In the end it will turn out that you have assigned to a global - _val
          - property and so added it to the scope chain (as the global object is
          at the end of all scope chains), but the code that does that is not
          shown above.

          Comment

          • andrew.bell.ia@gmail.com

            #6
            Re: &quot;this&quot ; in ctor

            On Sep 9, 9:36 am, Henry <rcornf...@rain drop.co.ukwrote :
            On Sep 9, 3:00 pm, andrew.bell...@ gmail.com wrote:
            >
            >
            >
            On Sep 9, 8:46 am, Henry wrote:
            On Sep 9, 2:36 pm, andrew.bell...@ gmail.com wrote:
            <snip>
            >
            >Why does access to the _field variable fail without the
            >"this"?  I thought that once _field was added to the
            >prototype object (an Integer, in this case), it was
            >just like any other property of the object (like _val,
            >which is set in the Integer ctor).
            ><snip>
            >  writeln("Foo field = " + _field + "!");}
            ><snip>
            >test2.js:11     ReferenceError: _field is not defined
            >
            Unqualified Identifiers such as - _field - are resolved
            against the scope chain not the prototype chain. There
            is no - _field - on the scope chain so you get an error
            when attempting to read its value in that way.
            >
            I get that, but then why does the access to _val succeed?
            >
            The code you have posed is not representative of the issue (it cannot
            even produce the error you have reported as it does not include any
            calls to - Foo -) and you will not get that question answered until
            you provide code and context that can be used to reproduce the issue.
            >
            In the end it will turn out that you have assigned to a global - _val
            - property and so added it to the scope chain (as the global object is
            at the end of all scope chains), but the code that does that is not
            shown above.
            This was the code in its entirety.

            I misunderstood the minimal documentation of the js interpreter I was
            using. It was unclear that when a program was run, the previous state
            was not cleared, thus the odd behavior.

            Sorry to have been a bother.

            Comment

            • Thomas 'PointedEars' Lahn

              #7
              Re: &quot;this&quot ; in ctor

              andrew.bell.ia@ gmail.com wrote:
              Integer = function(i)
              {
              this._val = i;
              }
              >
              Foo = function()
              {
              writeln("This Foo value = " + this._val + "!")
              writeln("Foo value = " + _val + "!")
              writeln("This Foo field = " + this._field + "!");
              writeln("Foo field = " + _field + "!");
              }
              First of all, there is no need to resort to function expressions when a
              function statement suffices:

              function Integer(i)
              {
              this._val = i;
              }

              Second, especially in the light of current standardization efforts,
              `Integer' is a user-defined identifier that is unwise to choose at best.

              Third, you should declare your identifiers so that they do not become
              properties of any object in the scope chain (which would be error-prone):

              var MyInteger = ...;
              Foo.prototype = new Integer(1);
              Unless it were your intention that all `Foo' objects would have their `_val'
              property initialized with the value 1, this is not how a prototype chain is
              properly set up; see the archives.

              Contrary to popular belief, your `Foo' objects would inherit from an
              initialized Integer object at first, not directly from the object
              `Integer.protot ype' refers to as it should be. You were looking for
              something along the following instead:

              function inheritFrom(Con structor)
              {
              function Dummy() {}
              Dummy.prototype = Constructor.pro totype;
              return new Dummy();
              }

              Foo.prototype = inheritFrom(Int eger);


              PointedEars
              --
              var bugRiddenCrashP ronePieceOfJunk = (
              navigator.userA gent.indexOf('M SIE 5') != -1
              && navigator.userA gent.indexOf('M ac') != -1
              ) // Plone, register_functi on.js:16

              Comment

              Working...