- bind是ES5中新定义的方法,作用是改变调用它的函数内部的this指向。
- 可以传递两个参数,第一个参数函数内this的指向,第二个及之后参数则作为函数的参数调用。
- 调用该方法会返回一个函数实例,就是改变this指向后的新函数。
function test1(...params){
console.log(this.abc)
console.log(params)
}
const obj={
abc:123
}
// 在window上也添加一个abc属性
window.abc=789;
// 使this指向obj,并传参数
const test2=test1.bind(obj,4,5,6)
test1(); // 789 undefined
test2(); // 123 [4, 5, 6]