2009/3/31 Stephan Beal <[email protected]>

>
> On Tue, Mar 31, 2009 at 3:32 PM, Kasper Lund <[email protected]> wrote:
> > 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.
>
> My gawd, it works:
>
>   function SubType()
>    {
>        this.__proto__ = new MyNative();
>        print('proto =',this.prototype,'str=',this.str);
>         return this;
>    }
>
>    var sub = new SubType();
>     sub.str = "sub.str";
>    var sub2 = new SubType();
>    sub2.str = "sub2.str";
>    print(sub.str,sub2.str,sub.me(),sub2.me());
>    print(sub.hi(),sub2.hi());
>    print( sub2 instanceof MyNative );
>
> Cool!
>
> i know this is a magic/unportable solution, but is there any real
> likelyhood that __proto__ will be removed, or only be available with
> certain build- or context-specific options? Or is there another
> (portable, though possibly hackish?) approach to doing this?
>

__proto__ is the only way to go. To be more specific, this is *exactly* what
happens when calling "new Constructor()" - inst.__proto__ is set to
Constructor.prototype.

However, not all JS engines permit __proto__ to be set by user (IE for
instance forbids this).

O.





>
> Thank you, Kasper!
>
> --
> ----- stephan beal
> http://wanderinghorse.net/home/stephan/
>
> >
>

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

Reply via email to