1、都是用来改变函数的this对象的指向的。
2、第一个参数都是this要指向的对象。
3、都可以利用后续参数传参。
例子:
// 声明一个对像xw
var xw = {
name: "小王",
gender: "男",
age: 24,
// 声明一个函数say
say: function () {
alert(this.name + " , " + this.gender + " ,今年" + this.age);
}
}
// 声明一个对象xh
var xh = {
name: "小红",
gender: "女",
age: 18
}
// 通过对象.属性名调用say()函数
xw.say();
调用xw的say方法显示xh的内容
1)call调用 改变this指向指向xh
xw.say.call(xh)
2)apply调用 改变this指向指向xh
xw.say.apply(xh)
3)bind调用 改变this指向指向xh 返回值是一个函数 要加()来调用
// 上面这样写是错误的
xw.say.bind(xh)
// 下面才能调用
xw.say.bind(xh)()