从csdn博客发现的,记录一下:
第一种方案:
function Rect(config){}
Rect.prototype.area = function(){
alert("我是父方法");
}
function myRect(config){
arguments.callee.prototype.constructor.prototype.area(); //子类里调用父方法area
arguments.callee.prototype.area();//子类里调用重载方法area
}
myRect.prototype = new Rect();
myRect.prototype.area = function(){
alert("我是重载方法");}
var rectObj = new myRect();
rectObj.constructor.prototype.area();//子类实例调用父类方法area
rectObj.area();//子类实例调用子类方法area
第二中方法:
function Rect(config){
this.width = config.width;
this.height = config.height;
this.area = function(){
alert(11);
}
}
function myRect(config){
this.superClass = Rect;
this.superClass(config);
this.superArea =this.area;
this.name = config.name;
this.area = function(){
this.superArea();
//这里如何调用一下父类的area方法?
alert(22);
//最终效果,是先alert(11),然后alert(22);
}
}
var a =new myRect({width:1,heigth:2,name:3});
a.area();
JS原型链(子类调用父类的方法)
最新推荐文章于 2022-04-16 13:10:27 发布