For example, can I write some function (call it convert
) that can convert an arrow function to a normal function? I want to bind the function to some object but I can’t do that for arrow functions. For example:
function test1() { console.log(this.a); }
const test2 = () => { console.log(this.a); }
const obj = { a: 4 };
function foo(f) {
(f.bind(obj))();
}
foo(test1); //outputs 4 because `this` is `obj`
foo(test2); //outputs undefined because `this` is the window
I would like to write a function convert
such that foo(test2)
also outputs 4.
I initially tried something like
function convert(f) {
return function() { f(); }
}
but that didn’t seem to work.
3