473,734 Members | 2,375 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.__paren t__.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 1766
<pi************ ****@gmail.comw rote:
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
15937
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
3193
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
2183
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 function that the this pointer is being used in? That is, is the type of the this pointer always determined entirely syntactically (and never dynamically)? Example: if a member function is invoked on an object of class Apple (appleobj.drip_it() ),...
3
1274
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 here? Thanks! private void InitializeComponent() { this.Load += new System.EventHandler(this.Page_Load); }
8
2238
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). I would like to write also in the file, the class that has sent the message. all the clases that need to send a message, derives from a base clas: class I_want_To_Send : public I_will_write_In_Logger
4
4167
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? class Parent { Parent::Parent() { child = new Child( this) }; Child *child; };
10
2738
by: Angel Tsankov | last post by:
Hello! Is the following code illformed or does it yield undefined behaviour: class a {}; class b {
6
2325
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 '*'
2
1448
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, unmanaged code. Everything is humming, but I have the following problem I cannot figure out. Say we have the following: template <typename Tclass SLL { public: SLL<T>* AddHead(T value);
0
8946
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, 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...
0
8776
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
9449
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
9310
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
9236
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
9182
tracyyun
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...
1
6735
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6031
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();...
2
2724
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.