Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Strange thing with "this" pointer

Reply
Thread Tools

Strange thing with "this" pointer

 
 
piotr.korzeniewski@gmail.com
Guest
Posts: n/a
 
      02-04-2007
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!

 
Reply With Quote
 
 
 
 
Richard Cornford
Guest
Posts: n/a
 
      02-04-2007
<(E-Mail Removed)> wrote:
> 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Pointer to pointer or reference to pointer A C++ 7 07-05-2011 07:49 PM
Pointer to pointer Vs References to Pointer bansalvikrant@gmail.com C++ 4 07-02-2009 10:20 AM
passing the address of a pointer to a func that doesnt recieve a pointer-to-a-pointer jimjim C Programming 16 03-27-2006 11:03 PM
one thing solved, but other terrible thing occur... Zam ASP General 1 03-14-2005 06:09 PM
Pointer-to-pointer-to-pointer question masood.iqbal@lycos.com C Programming 10 02-04-2005 02:57 AM



Advertisments