On Tue, Mar 31, 2009 at 2:27 PM, Stephan Beal <[email protected]> wrote: > > Hi, all! > > Today i figured out that i could extend my class binding framework > with 6 lines of code to allow JS-side classes to extend native bound > classes (i only had to change how the native "this" object is searched > for, recursively checking the prototype object). > > i've *almost* got this working, but i'm having a problem understanding > some JS behaviour. > > For this example, assume MyNative is a C++ type bound to JS, and it > has a native function, hi(), which is bound to JS. The following > *almost* does what i want, and i'll explain afterwards why it's wrong, > and will ask for help on understanding why my preferred approach isn't > working: > > > function SubType() > { > return this; > } > SubType.prototype = new MyNative(); > var sub = new SubType(); > print(sub.hi()); // prints "hi!" > > That works, but the prototype of all SubType objects is a single, > shared object, which is obviously going to cause me grief if i'm > subclassing a Window type (all instances would share the same native > window!). > > What i'd LIKE to do is: > > function SubType() > { > this.prototype = new MyNative(); > return this; > } > var sub = new SubType(); > print(sub.hi());
Super quick and very short reply: You can't change the prototype of an instance by assigning to the "prototype" property. You need to write to the (magical) __proto__ property, which is supported - though not encouraged - by V8. Cheers, Kasper --~--~---------~--~----~------------~-------~--~----~ v8-users mailing list [email protected] http://groups.google.com/group/v8-users -~----------~----~----~----~------~----~------~--~---
