"this" in ctor

A

andrew.bell.ia

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
 
H

Henry

On Sep 9, 2:36 pm, (e-mail address removed) wrote:
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).
writeln("Foo field = " + _field + "!");}
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.
 
A

andrew.bell.ia

On Sep 9, 2:36 pm, (e-mail address removed) wrote:




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,
 
G

Gregor Kofler

(e-mail address removed) 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
 
H

Henry

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.
 
A

andrew.bell.ia

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.
 
T

Thomas 'PointedEars' Lahn

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,247
Latest member
crypto tax software1

Latest Threads

Top