JS原型链(子类调用父类的方法)

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值