0. 结论
三者都是用来修改方法 this 指向的,call 与 apply 只是参数传入形式的区别,call 接收一个参数列表,apply 接收一个数组;而 bind 返回的是一个新函数,需要手动调用。
| 接受参数类型 | 接受参数详细 | 返回 | |
|---|---|---|---|
| call | 参数 | call(绑定的实例, 参数1, 参数2) | |
| apply | 参数组成的数组 | apply(绑定的实例, [参数1, 参数2]) | |
| bind | 绑定的实例 | bind(绑定的实例) | 绑定好的方法 |
1. api
var str = 'window'function Foo() {this.str = 'Foo'}function test(name, value) {console.log(this.str)console.log(value)}test(['a', 'b']); // window, undefinedtest.apply(new Foo(), ['a', 'b']); // Foo, btest.call(new Foo(), 'a', 'b'); // Foo, btest.bind(new Foo())('a', 'b'); // Foo, b
