JS封装继承函数
1 function extend(child,parent){ 2 var F=function(){} 3 F.prototype=parent.prototype; 4 child.prototype=new F(); 5 child.prototype=child; 6 child.uber=parent.prototype; //uber指向父对象的原型。方便子对象调用父对象原型中的重写的方法。 7 } 8 function Shape(){} 9 Shape.prototype.name="Shape"; 10 Shape.prototype.color="blue"; 11 12 function circle(radius){ 13 this.radius=radius; 14 } 15 16 circle.prototype.name="circle"; 17 circle.prototype.getS=function(){ 18 return this.radius*this.radius*3.14; 19 } 20 extend(circle,Shape); 21 22 var cir=new circle(2); 23 alert(cir.name); //"circle" 24 alert(cir.color); //"blue" 25 alert(cir.uber.name); //"Shape" 26 alert(cir.getS()) //"12.56"