一個案例理解 apply(thisObj)

 call 和apply 功能神似,參數不同

function.apply([thisObj[,argArray]])

function.call([thisObj[,arg1[, arg2[, [,.argN]]]]])

 


function A(){
        this.flag = 'A';
		this.flaf='C';
        this.tip = function(){
            alert("flag"+this.flag);			
        };
		console.log(this);
		alert("flaw"+this.flaw);
		alert("flaf"+this.flaf);
    }
    function B(){
        this.flag = 'B';
		this.flaf='D';
    }
	
	A.prototype.flaw="E";
	B.prototype.flaw="F";
	A();//執行函數A this指向window,this.flaw 未定義
    var a = new A(); // 執行函數A this指向函數A,a=等於函數本身,a不是函數不能執行, this.flaw="E",其中this.flaw是原生屬性,在this._proto_裡面,a.flag='A' a.flaf='C'
    var b = new B();	
	a.flaX="G";
	b.flaX="H";
    A.apply(b);// 執行函數 A ,這是this.flag,this.flaf被重新賦值,this.flaX='H' this.flaw="F",其中this.flaw是原生屬性,在this._proto_裡面
	a.tip.apply(b);// 執行函數tip,this.flag未重新賦值,this.flag='B',this.flaw="F"

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值