473,746 Members | 2,703 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

"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
Sep 9 '08 #1
6 1940
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...@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,
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...@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.
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.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
Sep 9 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2782
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"> <!-- window.open ('photos01.html','photogallery',config='height=550, width=750,toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, directories=no, status=no');
3
4225
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 HREF="http://www.familytreedna.com/surname_join.asp?code=Q17978" STYLE="TEXT-DECORATION: NONE;"> <FONT COLOR="RED" FACE="COPPERPLATE GOTHIC BOLD">Right Here</FONT></A></BUTTON></CENTER></P>
14
2258
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 classes A, ... typedef A * APtr;
1
2007
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
1299
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) The XML file: <?xml version="1.0"?>
5
2475
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); When attempting to compile this code, a message is returned that states that the "this" keyword is not available in this context.
7
2248
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 me. So, I'm here for an answer (more of a confirmation), hopefully. First let me say that I know people keep saying; it doesn't work because the member "is a private". I believe there's more to it than just simply that... Theory: You inherit,...
10
4797
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 if value of the current field is not the same value of the next row. eg:
6
2326
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 ClassB. But I get this Error from the compiler: and it's in ClassB... Error from the compiler: error C2061: syntax error : identifier 'TestA' error C2143: syntax error : missing ';' before '*'
0
8800
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9500
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9349
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9285
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8227
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6060
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4836
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3292
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2199
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.