嵌套函数的this指向问题
嵌套函数不会包含函数的this值!!!
如果嵌套函数被当做方法调用,this就指向调用他的对象
若果嵌套函数被当做函数调用,this就指向全局对象或者undefined
ES6之前,解决办法可以使用bind来改变其this指向
ES6之后,解决这个方法的一个技巧是使用箭头函数,箭头函数会继承外部的this值
let o = { m: function(){ let self = this this === o // true const f1 = function (){ this === o // false self === o // true this === globalThis //true } f1() const f2 = ()=>{ this === o // true this === globalThis //false } f2() const f3 = (function(){ this === o // true this === globalThis //false }).bind(this) f3() } }

浙公网安备 33010602011771号