The argument 10 gets passed to the anonymous inner function, bypassing the outer function. What is the principle here?
function aFunc() {
let firstNum = 2;
return (secondNum) => secondNum * firstNum;
}
let aVar = aFunc();
console.log(aVar(10));
The argument 10 gets passed to the anonymous inner function, bypassing the outer function. What is the principle here?
function aFunc() {
let firstNum = 2;
return (secondNum) => secondNum * firstNum;
}
let aVar = aFunc();
console.log(aVar(10));
Because aVar
is (secondNum) => secondNum * firstNum
where firstNum
and the closure has firstNum = 2
.
function aFunc() {
let firstNum = 2;
return (secondNum) => secondNum * firstNum;
}
let aVar = aFunc();
Calling aFunc()
runs the function which creates a closure around firstNum
and returning the arrow function (secondNum) => secondNum * firstNum
.
The subsequent call to aVar(10)
calls this arrow function and the result of adding firstNum
(2) and secondNum
(10) is calculated and returned.
console.log(aVar(10));