Setting this.prototype will not have any effect because the prototype
is only used to create new instances of objects. Observe:
function SubType()
{
this.prototype = new MyNative();
//note: although it does no harm, there is actually no need to return
this
}
var sub = new SubType();
sub.hi(); //doesn't exist...
var sub2 = new sub(); //now sub.prototype is used
sub2.hi(); //should exist.
Also, changing the prototype of the instance will probably change the
prototype of all instances.
I'd also like to be able to extend native classes, but I'm not sure
how I'd do it.
Alex
On Mar 31, 2009, at 8:27 AM, Stephan Beal 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());
>
> But for some reason that's then failing with:
>
> TypeError: Object #<a SubType> has no method 'hi'
>
>
> Why does the former variant of setting the prototype work, but the
> latter does not? How can i achieve the effect i'm looking for? Keep in
> mind that the constructor for a given native type might require
> certain arguments which only the client can provide, so i cannot
> generically provide some default instance of such a type.
>
> :-?
> >
Alex Iskander, TPSi
--~--~---------~--~----~------------~-------~--~----~
v8-users mailing list
[email protected]
http://groups.google.com/group/v8-users
-~----------~----~----~----~------~----~------~--~---