将此文件从.call()传递给箭头功能 [英] Passing this from .call() to arrow function
本文介绍了将此文件从.call()传递给箭头功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个箭头功能,我试图用 call()
执行。为简化起见,如下:
I have an arrow function that I am trying to execute with call()
. For the sake of simplification, as follows:
按预期运作
const func = (e) => {
console.log(e)
}
func.call(null, e)
嗯...这里发生了什么?
我希望以下代码能够通过元素
进入 func
as this
。
I would expect the following code to pass element
into func
as this
.
const func = (e) => {
console.log(this)
console.log(e)
}
func.call(element, e)
但是,此
仍为 undefined
。
如果我将其切换为常规函数定义,则所有内容都按预期工作。
If I switch it to a regular function definition, all works as expected.
const func = function (e) {
console.log(this)
console.log(e)
}
func.call(element, e)
问题
为什么我无法将此
的上下文从 call()传递给箭头函数
?
Why am I not able to pass a context for this
into an arrow function from call()
?
推荐答案
此
未绑定在箭头函数中,所以 call()
和 apply()
只能传递参数。 忽略此
this
is not bound in arrow functions, so call()
and apply()
can only pass in parameters. this
is ignored
这篇关于将此文件从.call()传递给箭头功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文