js實現繼承機制

function extend(target,parent,params){  
    parent.apply(target,params);  
    var p = null,o;  
    for(p in parent.prototype){  
        o = target.constructor.prototype;  
        if(!o[p]){  
            o[p] = parent.prototype[p];  
        }  
        o[p]["super"] = parent.prototype;  
    }  
};  
 

function Person(name){  
    this.name = name;  
};  
Person.prototype.getName = function(){  
    alert(this.name);  
}  
function Student(name){  
    extend(this,Person,[name]);  
};  
 

 

 

var stu = new Student("lynn");  
        stu.getName();  

 

發佈留言