I want to know the difference between the following two block of codes:
function foo() {
var a = 'private variable';
return function a(){
console.log(a)
}
}
foo()(); // output: function a(){...}
vs
function foo() {
var a = 'private variable';
function a(){};
return () => {console.log(a)}
}
foo()(); // output: private variable
In the first block of code, based on the hoisting, the function a definition should be hoisted, and then var a = 'private variable'
rewrite the a
, but why the console.log(a)
output the function definition?