By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,466 Members | 1,167 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,466 IT Pros & Developers. It's quick & easy.

"this" in ctor

P: n/a
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.js");
This Foo value = 1!
Foo value = 1!
This Foo field = foo field!
test2.js:11 ReferenceError: _field is not defined
Sep 9 '08 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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.
Sep 9 '08 #2

P: n/a
On Sep 9, 8:46*am, Henry <rcornf...@raindrop.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,
Sep 9 '08 #3

P: n/a
an************@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
Sep 9 '08 #4

P: n/a
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.
Sep 9 '08 #5

P: n/a
On Sep 9, 9:36*am, Henry <rcornf...@raindrop.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.
Sep 9 '08 #6

P: n/a
an************@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.prototype' refers to as it should be. You were looking for
something along the following instead:

function inheritFrom(Constructor)
{
function Dummy() {}
Dummy.prototype = Constructor.prototype;
return new Dummy();
}

Foo.prototype = inheritFrom(Integer);
PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
Sep 9 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.