Presented here is a slightly more advanced function.
The following code functions as intended:
var square = (a) => a * a;
var callAndLog = (func) => {
return function () {
var res = func.apply(undefined, arguments);
console.log("Result is: " + res);
return res;
}
};
var squareAndLog = callAndLog(square);
squareAndLog(5); // Result is 25
However, substituting the arrow function does not produce the desired outcome:
var square = (a) => a * a;
var callAndLog = (func) => {
return (() => {
var res = func.apply(undefined, arguments);
console.log("Result is: " + res);
return res;
})
};
var squareAndLog = callAndLog(square);
squareAndLog(5); // Result is NaN
The use of arrow functions can be tricky due to their loose nature. I've attempted to resolve this by using parentheses in various ways without success.