473,388 Members | 1,526 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

"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.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
6 1924
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Michael Stevens | last post by:
Probably the wrong wording but since I'm not a scripter I won't claim to know what I'm talking about. I got this script from www.htmlgoodies.com <script language="JavaScript"> <!--...
3
by: Hodad | last post by:
I would like to adapt, as much as possible, the appearance and color red of the font in this button: <P><CENTER><BUTTON VALUE="SUBMIT"><A...
14
by: Ernst Murnleitner | last post by:
Dear Readers, Is it possible to forbid conversion from this or use of this in general except where it is explicitly wanted? Reason: I changed my program from using normal pointers to...
1
by: tnhoe | last post by:
Hi, <Form method='post' action="next.htm?btn="+"this.myform.myobj.value"> What is the correct syntax for above ? Regards Hoe
1
by: Shapper | last post by:
Hello, I am accessing a value in a XML value: news.Load(Server.MapPath("xml/ news.rss")) newslabel.Text = CType(news.SelectSingleNode("rss version=&quot;2.0 &quot;/channel/title").InnerText, String) ...
5
by: ChrisB | last post by:
Hello: An object that is a field in another object has a constructor that requires a reference to the containing object: // object fields ChildObject childObject = new ChildObject(this); ...
7
by: relient | last post by:
Question: Why can't you access a private inherited field from a base class in a derived class? I have a *theory* of how this works, of which, I'm not completely sure of but makes logical sense to...
10
by: craig.keightley | last post by:
I am trying to get the next row within a loop for a script i am developing... I need to display a final table row within the table that i have displayed on the page, but i only want to show it...
6
by: babakandme | last post by:
Hi to every body...:D I'm a novice C++ programmer & I've a question, I have the ClassA & in it's constructor, I instantiate ClassB, and I want send "this" pointer """pointer to ClassA""" to the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.