模拟类式继承

//模拟类式继承
//Douglas Crockford
Function.prototype.method = function(name,func) {
    this.prototype[name] = func;
    return this;
};

Function.method('inherits', function(parent) {
    var depth = 0;
    var proto = this.prototype = new parent();
    this.method('uber', function uber(name)) {
        var func;
        var ret;
        var v = parent.prototype;
        if(depth) {
            //上溯必要的depth,找到原始的prototype
            for(var i=d;l i>0; i+=1) {
                v = v.constructor.prototype;
            }
            func = v[name];    
        } else {
            func = proto[name];
            if(func == this[name]) {
                func = v[name];
            }
        }
        depth +=1 ;
        ret = func.apply(this,Array.prototype.slice.apply[arguments,[1]]);
        depth -= 1;
        return ret;
    });
    return this;
});

Function.method('swiss', function(parent)) {
    for(var i=1; i<arguments.length; i+=1) {
        var name = arguments[i];
        this.prototype[name] = parent.prototype[name];
    }
    return this;
});

 

posted @ 2013-10-26 15:33  LambdaTea  阅读(149)  评论(0编辑  收藏  举报