最佳继承扩展方法

<!DOCTYPE>
<html>
    <head></head>
    <body>
<script type="text/javascript">
void function(){
    //oo
    var superTest = /\b_super\b/;
    Function.prototype.derive = function(constructor, proto){
        if(typeof constructor === 'object'){
            proto = constructor;
            constructor = proto.constructor || function(){};
            delete proto.constructor;
        }
        var parent = this;
        var fn = function(){
            parent.apply(this, arguments);
            constructor.apply(this, arguments);
        };
        var tmp = function(){};
        var addSuper = function(name, fn, parent) {
            return function() {
                var temp = this._super;
                this._super = parent[name];
                var ret = fn.apply(this, arguments);
                this._super = temp;
                return ret;
            }
        }
        tmp.prototype = parent.prototype;
        var fp = new tmp(),
            cp = constructor.prototype,
            pp = parent.prototype,
            key;
        for(key in cp){
            if(cp.hasOwnProperty(key)){
                fp[key] = 
                (typeof cp[key] === 'function' && typeof pp[name] === 'function' && superTest.test(cp[key])) ? addSuper(key, cp[key],pp) :
                    cp[key];
            }
        }
        proto = proto || {};
        for(key in proto){
            if(proto.hasOwnProperty(key)){
                fp[key] = 
                    (typeof proto[key] === 'function' && (typeof cp[key] === 'function' ||  typeof pp[key] === 'function') && superTest.test(proto[key])) ? addSuper(key, proto[key], typeof cp[key] === 'function' ? cp : pp) :
                    proto[key];
            }
        }
        fp.constructor = constructor.prototype.constructor;
        fn.prototype = fp;
        return fn;
    };    
}();
</script>    

<script>
    //EXAMPLE
    var ClassParent = Object.derive(function(){
        console.log('hi,classParent init');
    }, {
        run: function(param){
            console.log('hi classParent run:',param);
        }
    });
    var Class = ClassParent.derive(function(){
        console.log('hi,class init');
    },{
        run: function(param){
            this._super(param);
            console.log('hi class run:',param);
        },
        say: function(param){
            console.log('hi class say:',param);
        }
    });

    var ChildClass = Class.derive(function(){
        console.log('hi,classChild init');
    }, {
        run: function(param){
            this._super(param);
            console.log('hi childClass run:',param);
        },
        say: function(param){
            this._super(param);
            console.log('hi childClass say:',param);
        },
        eat: function(param){
            console.log('hi childClass eat:',param);
        }
    })
    
    var child = new ChildClass;
    child.run('runParam');
    child.say('sayParam');
    child.eat('eatParam');

console.log('---------------------------------');
    var OtherClass= Object.derive(function(){
        console.log('hi,otherClass  init');
    }, {
        listen: function(param){
            console.log('hi otherClass listen:',param);
        }
    });

    var OtherChildClass = OtherClass.derive(ChildClass, {
        run: function(param){
            this._super(param);
            console.log('hi OtherChildClass  run:',param);
        },
        say: function(param){
            this._super(param);
            console.log('hi OtherChildClass say:',param);
        },
        eat: function(param){
            this._super(param);
            console.log('hi OtherChildClass eat:',param);
        },
        listen: function(param){
            this._super(param);
            console.log('hi OtherChildClass listen:',param);
        }
    });

    var otherchild = new OtherChildClass 
    otherchild.run('runParam');
    otherchild.say('sayParam');
    otherchild.eat('eatParam');
    otherchild.listen('listenParam');
</script>
</body>
</html>

 

posted @ 2013-10-07 21:45  一只柯楠  阅读(279)  评论(0编辑  收藏  举报