I think I found this at KevinLinDev
(http://www.kevlindev.com/tutorials/javascript/inheritance/index.htm).

Object.prototype._extends_ = function( ChildClass, ParentClass )
{
   function TempClass() {}
   TempClass.prototype = ParentClass.prototype;

   ChildClass.prototype = new TempClass();
   ChildClass.prototype.constructor = ChildClass;
   ChildClass._superConstructor_ = ParentClass;
   ChildClass._superClass_ = ParentClass.prototype;
};

And you can use it this way.

var MyType = function()
{
   this.prop1 = 1;
};

var MySubType = function()
{
   this._superConstructor_.call( this );
   this.prop2 = 2;
};

Object._extends_( MySubType, MyType );

var x = new MySubType();

print( x.prop1 + ":" + x.prop2 );

Key is that TempClass that allows you to swap in your parent class.
And I don't see why you couldn't do this for a native-bound type.

-Louis

On Tue, Mar 31, 2009 at 7:04 AM, Stephan Beal <[email protected]> wrote:
>
> 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?
>
> 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