javascript中的类式继承

//辅助函数,让你能够将新函数绑定到对象的prototype上
//为Function.prototype增长一个公有方法。全部由类的扩充的函数均可以使用它,
//它返回this,当我写一个不须要返回值的方法时,我一般让它返回this,这顾虑到了串联样式
Function.prototype.method = function (name, func) {
    this.prototype[name] = func;
    return this;
};

//inherits方法,它的做用是让一个类从另外一个上继承
//这一函数能够提供简单的单对象继承,他的代码主要围绕在任意对象方法中调用this.uber('methodName')为中心,并再让
//这个uber方法去执行他要覆盖的父对象的方法
//容许从其余对象继承函数,同时仍然能够调用属于父对象的那些函数
Function.method("inherits", function (parent) {
//记录目前所在父层次的级数
 var d = {}, 
//继承父对象的方法
p = (this.prototype = new parent());
//建立一个新的名为'uber'的"特权函数",调用它时会执行全部在继承时被重写的函数
 this.method("uber", function uber(name) {
        if (!(name in d)) {
            d[name] = 0;
        }   
//f为要执行的函数,r为函数的返回值,v为父对象的prototype     
        var f, r, t = d[name], v = parent.prototype;
//若是uber被调用的时候,会进入到这一步
         if (t) {
            while (t) {
//上溯到必要的d[name],以找到原始的prototype
                v = v.constructor.prototype;
                t -= 1;
            }
//从该prototype中获取函数
 f = v[name];
        } 
//不然是'uber'的第一次调用       
  else {
//从prototype得到要执行的函数           
 f = p[name];
//若是此函数属于当前的prototype   
 if (f == this[name]) {
//则改成调用父对象的prototype
 f = v[name];
            }
        }
//记录咱们在继承堆栈中所在位置的级数
 d[name] += 1;
//使用除第一个之外的全部的arguments 并调用此函数,由于第一个参数是执行的函数名
 r = f.apply(this, Array.prototype.slice.apply(arguments, [1]));
//恢复继承堆栈
 d[name] -= 1;
//返回执行过的函数的返回值
 return r;
    });
    return this;
});

//swiss方法自始自终循环arguments参数,对于每个名称,它都复制出一个成员从父类的原型上到新类的原型上。
//只继承父对象特定函数的函数,而非使用new parent(),继承全部函数
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;
});

方法的使用
function Parenizor(value) {

}
Parenizor.method("toal", function () {
    return("haode");
});
function mParenizor(value) {

}
mParenizor.inherits(Parenizor);
mParenizor.method("toal", function () {
       var a=3;
       return this.uber("toal");
 });	
    
function fParenizor(value) {
   

}
fParenizor.inherits(mParenizor);
 fParenizor.method("toal", function (name) {
        var a=2;
       return this.uber("toal","haerbin");
 });	

 function hParenizor(value) {
   
}
hParenizor.inherits(fParenizor);
hParenizor.method("toal", function () {
       var a=1;
       return this.uber("toal","lilei");
 });	

var myZParenizor = new hParenizor("laoli");
     var myString = myZParenizor.toal();

new子对象的实例,而后调用toal方法,toal方法执行到return this.uber("toal","lilei")这句的时候,会调用inherits函数
的uber方法,因为此方法第一次执行,因此d[toal]=0,而后进入f=p[toal],因为当前对象有toal方法,向下执行,将f置为
父对象的toal方法,计数器d[toal]加1,而后执行r = f.apply(this, Array.prototype.slice.apply(arguments, [1]));
此方法将参数传递给f函数,并将指针改成this,来执行父对象的toal方法,而后父对象的toal方法被执行,
fParenizor.method("toal", function (name) {//父对象的toal方法被执行
        var a=2;
       return this.uber("toal","haerbin");
 });	
当执行到 return this.uber("toal","haerbin") 因为第二次执行此方法,因此d[toal]为1,而后进入while循环,
 经过v = v.constructor.prototype将直接找到v的原始的prototype,执行原始prototype上的toal方法,
Parenizor.method("toal", function () {
    return("haode");
});
而后将返回值赋给r,而后将d[toal]减1(因为已经找到原始的toal方法,d[toal]值应为1,因此进行自减).
这是我对类式继承的理解,不知道有没有高手帮我看下,给修改或补充下呗
相关文章
相关标签/搜索