Strange thing with "this" pointer

Discussion in 'Javascript' started by piotr.korzeniewski@gmail.com, Feb 4, 2007.

  1. Guest

    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, 2007
    #1
    1. Advertising

  2. <> 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.
     
    Richard Cornford, Feb 4, 2007
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Shapper
    Replies:
    2
    Views:
    430
    Shapper
    May 20, 2005
  2. mikael hellström

    question about a strange thing

    mikael hellström, Sep 9, 2003, in forum: HTML
    Replies:
    1
    Views:
    357
    PeterMcC
    Sep 9, 2003
  3. Paul Watt

    strange thing happening with list

    Paul Watt, Jul 8, 2005, in forum: HTML
    Replies:
    8
    Views:
    419
    Paul Watt
    Jul 8, 2005
  4. Dan Bishop

    Re: shebang strange thing...

    Dan Bishop, Jun 24, 2003, in forum: Python
    Replies:
    19
    Views:
    1,142
    Ben Finney
    Jun 29, 2003
  5. Zam
    Replies:
    1
    Views:
    249
    Mark Schupp
    Mar 14, 2005
Loading...

Share This Page