类式继承——代理函数模式(代理构造函数模式)

function inhert(C, P) {
    var F = function() {};
    F.prototype = P.prototype;
    C.prototype = new F();
    C.uber = P.prototype;
    C.prototype.constructor = C;
}

进化版(优化版)

var inherit = (function{
    var F = function() {};
    return function(C,P) {
        F.prototype = P.prototype;
        C.prototype = new F();
        C.uber = P.prototype;
        C.prototype.constructor = C;
    }
}());

为了避免在每次需要继承时都创建临时构造函数。仅创建一次临时构造函数,并且修改它的原型,这已经是非常充分的。在具体实现方式上,可以使用即时函数并且在闭包中存储代理函数

posted @ 2015-11-09 11:23  targeral  阅读(298)  评论(0编辑  收藏  举报