Eta-conversion 正在考虑函数 (x) => f(x)
与函数 f
相同.在工作中重构代码时,我试图使用这种等效性来简化一些高阶函数调用。然而,事情并没有完全解决,而且我对实际发生的事情有些迷茫,在 Javascript 方面还是个新手。这是一个最小的例子,我希望能正确地举例说明我的问题。
const station = {
take: (f) => f(5),
love: function(car) {
this.take((x) => car.addFuel(x));
},
hate: function(car) {
this.take(car.addFuel);
}
};
const mercedes = {
fuel: 0,
addFuel: function(amount) {
this.fuel += amount;
}
};
station.love(mercedes);
console.log(mercedes.fuel);
// output: 5
station.hate(mercedes);
console.log(mercedes.fuel);
// output: 5; expected output: 10
从 eta 等价,我预计 love
和 hate
来自 station
的函数对象行为相同。但是,似乎 hate
函数实际上什么都不做。这里发生了什么?
最佳答案
this
存在范围界定问题.在您的原始代码中 hate
回调 this
指全局window
目的。您需要使用 Function.prototype.bind()为 this
绑定(bind)正确的范围.
const station = {
take: (f) => f(5),
love: function(car) {
this.take((x) => car.addFuel(x));
},
hate: function(car) {
this.take(car.addFuel.bind(car));
}
};
const mercedes = {
fuel: 0,
addFuel: function(amount) {
this.fuel += amount;
}
};
station.love(mercedes);
console.log(mercedes.fuel);
station.hate(mercedes);
console.log(mercedes.fuel);
关于javascript - Javascript 中的 Eta 转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64538840/