改变this指向的方法有哪些?
参考答案
call(thisArg, a, b):立即调用,参数逐个传apply(thisArg, [a, b]):立即调用,参数用数组bind(thisArg, ...args):返回新函数,永久绑定,可预设参数- 箭头函数:词法绑定,取定义处外层 this
- 调用方式:
obj.fn()隐式绑定;new Fn()绑定到新实例 - 事件监听:
addEventListener中 this 为绑定元素 - 某些 API 支持 thisArg 参数(forEach/map 的第二个参数)