ES6箭头函数,关于this指向的案例
const arrow = {
say: function () {
setTimeout(function () {
console.log(this)
}, 50)
},
sayWithThis: function () {
const that = this
setTimeout(function () {
console.log(that)
}, 500)
},
sayWithArrow: function () {
setTimeout(() => {
console.log(this)
}, 1500)
},
sayWithGlobalArrow: () => {
setTimeout(() => {
console.log(this)
}, 2000)
}
}
arrow.say()
arrow.sayWithThis()
arrow.sayWithArrow()
arrow.sayWithGlobalArrow()
输出:
与君共勉:再牛逼的梦想,也抵不住傻逼般的坚持!