涛神

导航

 

三个方法都是用来改变上下文(this)的:

  • call 和 apply用来绑定上下文后直接执行,不同在于call输入参数为普通形参形式,apply输入参数为数组形式
  • bind用来绑定上下文后返回可执行的函数

For example:

var a={
user:"tao",
fn:function(age){
console.log("hello "+this.user);
console.log("your age is "+age);
}
}
var b=a.fn;
b(15);//hello undefined your age is 15
a.fn.call(a,15);//hello tao your age is 15
a.fn.apply(a,[15]);//hello tao your age is 15
var c=a.fn.bind(a,15);
c(15);//hello tao your age is 15

posted on 2018-02-25 19:40  IT漂泊者  阅读(90)  评论(0)    收藏  举报