Not a bug in V8, just an oddity of JavaScript. Run the same code in
SpiderMonkey, for example, and you'll get the same results.

Basically, you need to set constructors manually if you want to use them the
way your code does. See e.g. this random article a Google search turned up,

http://phrogz.net/JS/Classes/OOPinJS2.html

and notice how they set the constructors manually.

- kripken


On Fri, Oct 16, 2009 at 3:59 PM, Joe Strout <[email protected]> wrote:

>
> This is either a bug in v8, or something I deeply misunderstand about
> how JavaScript is supposed to work.  Please consider the following:
>
> d8> Class1 = function() { this.foo=1; };
> function () { this.foo=1; }
> d8> Class2 = function() { this.foo=2; }
> function () { this.foo=2; }
> d8> Class2.prototype = new Class1();
> [object Object]
>
> ...so here, we've declared a base class Class1, and a subclass Class2.
>
> d8> c1 = new Class1();
> [object Object]
> d8> c2 = new Class2();
> [object Object]
>
> ...created an object of each class.
>
> d8> c2.constructor
> function () { this.foo=1; }
> d8> c2.constructor === Class1;
> true
> d8> c2.constructor === c1.constructor;
> true
>
> ...wtf?!?  c2's constructor property should point to Class2, shouldn't
> it?  Yet the 'foo' property was properly initialized:
>
> d8> c1.foo
> 1
> d8> c2.foo
> 2
>
> ...and other tests show that all inheritance is working as it should;
> properties added to Class1.prototype appear on both c1 and c2, while
> properties added to Class2.prototype appear on c2.  But how can this be,
> when c2's constructor points directly at Class1?
>
> I've tried this with deeper hierarchies as well: if we define a Class3
> which subclasses Class2, any objects we create also have their
> .constructor property pointing at Class1.  It's as though the
> .constructor property always walks all the way up the prototype chain
> and returns most "base" constructor.
>
> I've tried hard to understand why this might be correct, but I just
> can't see it.  If this isn't a bug in v8, can someone explain why?
>
> Thanks,
> - Joe
>
>
> >
>

--~--~---------~--~----~------------~-------~--~----~
v8-users mailing list
[email protected]
http://groups.google.com/group/v8-users
-~----------~----~----~----~------~----~------~--~---

Reply via email to