Strange problem with "this" pointer

P

Piotr K

I'm really confused, please take a look at code below:

function Class(obj)
{
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);

It should alert "Class A" and it does, but only in Internet Explorer
and Opera - strangly in Firefox it alerts "Class B".. If I remove line
"var c = C()" or move it above "var b = B()", Firefox also alerts
"Class A". I guess it has something to do with "this" pointer, but I
don't know what - theoretically it should work as it is (in fact it
doest, but as I mentioned only in IE and Opera)

Does anyone have an idea why is it working so strangly and what to do
with it ?

Thanks for any help
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top