摘自你不知道 JS - this 和对象原型(prototype),作者:Kyle Simpson,第 32 页,标题词法 this:
function foo() {
return (a) => {
console.log(this.a);
};
}
var obj1 = {
a:2
}
var obj2 = {
a:3
}
var bar = foo.call(obj1);
bar.call(obj2); // 2, not 3!
这里的要点是,这个返回的箭头函数将从函数 foo 继承词法 this。
我不明白的是为什么上面这个箭头函数的输入是a
?
- 我从箭头函数中删除了“a”,这个示例仍然按预期工作。
- 当通过引用调用箭头函数时,调用它们时不带输入参数。
将“a”作为箭头函数的输入有什么意义?
最佳答案
What is the point of having 'a' as the input to the arrow function?
正如你所说,它是一个从未使用过的变量声明。
这毫无意义。
ESLint 报告:
'a' is defined but never used. (no-unused-vars)
关于javascript - 将 'a' 作为此箭头函数的输入是错误吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60950924/