473,394 Members | 1,813 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,394 software developers and data experts.

Strange thing with "this" pointer

I'm really confused with this one, first please take a look at code
below:

function Class(obj)
{
if(!obj) obj = new Object();

obj.Extend = function(obj) {
this.__parent__ = new Object();

for(p in this) {
this.__parent__[p] = this[p];
}

for(p in obj) {
this[p] = obj[p];
}

return this;
}

return obj;
}

function A()
{
return Class({ name : "Class A" })
}

function B()
{
return A().Extend({ name : "Class B" });
}

function C()
{
return B().Extend({ name : "Class C" });
}

var a = A();
var b = B();
var c = C();

alert(b.__parent__.name);

Alright, this code is quite simple but one thing is really confusing -
it should alert "Class A" and it does in Internet Explorer and Opera,
but strangly in Firefox it alerts "Class B".. But what's even more
confusing - if I remove line "var c = C()", Firefox magically alerts
"Class A". What in the world has line "var c = C()" to do with "var b
= B()" ? The only thing that comes to my mind is this part of code:

[...]
this.__parent__ = new Object();

for(p in this) {
this.__parent__[p] = this[p];
}
[...]

But why does it works well in Internet Explorer and Opera? It doesn't
make any sense..

Thanks for any help!

Feb 4 '07 #1
1 1747
<pi****************@gmail.comwrote:
I'm really confused with this one, first please take a look at code
below:

function Class(obj)
{
if(!obj) obj = new Object();

obj.Extend = function(obj) {
this.__parent__ = new Object();
<snip>
Alright, this code is quite simple but one thing is really confusing -
it should alert "Class A" and it does in Internet Explorer and Opera,
but strangly in Firefox it alerts "Class B".
<snip>

Objects in JavaScript(tm) have a non-standard extension property called
"__parent__", which is probably read only. Your use of that name in your
code is probably causing a naming collision and unexpected results in
browsers suing JavaScript(tm).

Richard.

Feb 4 '07 #2

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

Similar topics

5
by: Rasti | last post by:
Hello, I am trying to translate a C++ script to Java. Are there any differences between the C++ this-pointer and the Java this-pointer? Thank you
32
by: Christopher Benson-Manica | last post by:
Is the following code legal, moral, and advisable? #include <iostream> class A { private: int a; public: A() : a(42) {}
9
by: aden | last post by:
I have read the years-old threads on this topic, but I wanted to confirm what they suggest. . . Can the this pointer EVER point to a type different from the class that contains the member...
3
by: Polaris | last post by:
I noticed in the ASP.NET web application, as shown below, the "this" pointer is used in the code generated by the Visual C# IDE. Anyone can explain why it is necessary to use the "this" pointer...
8
by: solarin | last post by:
Hi all. I'm writting a logger class to write all the debug/info/warning/error messages in a file. Every time a class needs to send any message, should send a code (int) and a message (string)....
4
by: craig | last post by:
During construction of an object "parent", if you create a subobject that stores a pointer to the parent (through the "this" pointer), will that pointer be valid when the subobject is later called?...
10
by: Angel Tsankov | last post by:
Hello! Is the following code illformed or does it yield undefined behaviour: class a {}; class b {
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...
2
by: Sergei Shelukhin | last post by:
Hi. I have barely written any C++ for past 3-4 years, I need to refresh it in my memory so I decided to do some coding starting with classic algos and data structures, using Visual Studio 9,...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
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,...
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
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...

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.