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 6 1956
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.
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, 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
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.
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. 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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');
|
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>
|
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;
|
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
|
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="2.0
"/channel/title").InnerText, String)
The XML file:
<?xml version="1.0"?>
| |
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.
|
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,...
|
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:
|
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 '*'
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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...
| |
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...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
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...
| |