You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
varo1={name: "lili",age: 20,func: function(country,city){console.log("My name is "+this.name+", I am "+this.age+" years old."+"I come from "+country+", "+city)}}varo2={name: "zzy",age: 22}o1.func("USA","NY");//My name is lili, I am 20 years old.I come from USA, NYo1.func.call(o2,"China","NJ");//My name is zzy, I am 22 years old.I come from China, NJ
apply
第一个参数是this要指向的新对象。后面的参数以数组的形式传递。apply方法是立即调用。
o1.func("USA","NY");//My name is lili, I am 20 years old.I come from USA, NYo1.func.apply(o2,["China","NJ"]);//My name is zzy, I am 22 years old.I come from China, NJ
o1.func("USA","NY");//My name is lili, I am 20 years old.I come from USA, NYo1.func.bind(o2,"China","NJ")();//My name is zzy, I am 22 years old.I come from China, NJ
The text was updated successfully, but these errors were encountered:
这三个函数都是为了改变调用函数的对象。也就是改变函数内部的this指向。
call
第一个参数是this要指向的新对象。后面的参数以参数列表的形式传递。call方法是立即调用。
apply
第一个参数是this要指向的新对象。后面的参数以数组的形式传递。apply方法是立即调用。
bind
第一个参数是this要指向的新对象。后面的参数以参数列表的形式传递。bind方法是返回函数,不是立即调用。因此,要写一个()才能调用。
The text was updated successfully, but these errors were encountered: