【发布时间】:2017-08-21 02:59:01
【问题描述】:
我有一个箭头函数,我想用call() 执行它。为简化起见,如下:
按预期运行
const func = (e) => {
console.log(e)
}
func.call(null, e)
嗯……这是怎么回事?
我希望以下代码将element 传递给func 作为this。
const func = (e) => {
console.log(this)
console.log(e)
}
func.call(element, e)
但是,this 仍然是 undefined。
如果我将其切换为常规函数定义,一切都会按预期工作。
const func = function (e) {
console.log(this)
console.log(e)
}
func.call(element, e)
问题
为什么我无法将 this 的上下文传递给来自 call() 的箭头函数?
【问题讨论】:
-
出于兴趣,您为什么决定使用箭头函数而不是常规函数?
-
@CodingIntrigue 没有理由,纯粹是我注意到这种行为的实验。
标签: javascript ecmascript-6 arrow-functions