Hi, all!

i know this topic has been talked about more than enough times, but i
recently took another look at it and i think i've found an approach to
subclassing which is non-intrusive enough to be useful (e.g. it
doesn't polute Object.prototype). i'll let the code and comments speak
for themselves:

//BEGIN CODE
/**
   Causes ChildClass to inherit from ParentClass, in the conventional
   OO sense of the word inherit.

   Unlike real-world inheritance, ParentClass must outlive ChildClass.

   Derived from code posted here:

   http://groups.google.com/group/v8-users/browse_thread/thread/adfc2978ee519b42

   Author: Stephan Beal (http://wanderinghorse.net/home/stephan/)
   License: Public Domain

    How to use this:

    function MyType(){...}
    function MySubType(){...}

    // Approach 1:
    MySubType.extendClass = extendClass;
    MySubType.extendClass( MyType );

    // Approach 2:
    extendClass( MySubType, MyType );


    After that:

    var sub = new MySubType(...);
    print(sub instanceof MyType ); // true

    and sub will have access to properties inherited from MyType.
*/
function extendClass( ChildClass, ParentClass )
{
    if( 1 == arguments.length )
    {
        return arguments.callee.apply( this, [this,ChildClass] );
    }
    function TempClass() {};
    TempClass.prototype = ParentClass.prototype;
    ChildClass.prototype = new TempClass();
    ChildClass.prototype.constructor = ChildClass;
    ChildClass._superConstructor_ = ParentClass;
    ChildClass._superClass_ = ParentClass.prototype;
};

if(1)
{ /* test/demo. Don't try this from a browser (where print() has very
different behaviour)!
    */
    var MyType = function()
    {
        var av = Array.prototype.slice.apply(arguments,[0]);
        print("ctor MyType(",av.join(','),')');
        this.prop1 = 1;
    this.func = function(){
        for( var k in this )
        {
            var v = this[k];
            print("this["+k+"] =",v);
        }
    };
    };

    var MySubType = function()
    {
        var av = Array.prototype.slice.apply(arguments,[0]);
        MySubType._superConstructor_.call( this, av );
        print("ctor MySubType(",av.join(','),')');
        this.prop2 = 2;
    };



    if(0) /* approach 1: */
    {
        MySubType.extendClass = extendClass;
        MySubType.extendClass( MyType );
    }
    else /* approach 2: */
    {
        extendClass( MySubType, MyType );
    }
    var x = new MySubType(7,3,11);
    x.func();
    print( "x.prop1:x.prop2 =",x.prop1 + ":" + x.prop2 );
    print( "x instanceof MyType ==",x instanceof MyType );

};

//END CODE

On my box both approaches demonstrated output:


ctor MyType( 7,3,11 )
ctor MySubType( 7,3,11 )
this[prop1] = 1
this[func] = function (){
        for( var k in this )
        {
            var v = this[k];
            print("this["+k+"] =",v);
        }
    }
this[prop2] = 2
this[constructor] = function ()
    {
        var av = Array.prototype.slice.apply(arguments,[0]);
        MySubType._superConstructor_.call( this, av );
        print("ctor MySubType(",av.join(','),')');
        this.prop2 = 2;
    }
x.prop1:x.prop2 = 1:2
x instanceof MyType == true

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

Reply via email to