更改this指向

call()    apply()    bind()

call()

改变this指向时,传入参数须为(需要指向的对象,参数1,参数2...)

 

apply() 

改变this指向时,传入参数为(需知向的对象,数组),应用与call()相似

 

bind()

应用于不需要马上执行的函数中改变this指向,setTimeout

1 var btn;
2 btn.onclick =  function(){
3   setTimeout(function(){
4      this.disable = false;        
5     }.bind(this),2000)     
6 }

bind(this)改变了setTimeout中的this.disable的this指向,使其指向btn

posted @ 2020-06-16 20:03  幻星缈宇  阅读(101)  评论(0)    收藏  举报