当前位置:编程学习 > JS >>

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();  

 


补充:web前端 , JavaScript ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,