Can't seem to figure out why its not able to access variable 'a':
var a = function(){
console.log('AAA');
}
(function(){
console.log(a);
})();
Can't seem to figure out why its not able to access variable 'a':
var a = function(){
console.log('AAA');
}
(function(){
console.log(a);
})();
The problem here is that you're trying to call a function as follow undefined()
, Why?
This is what is happening:
var a = function(){
console.log('AAA');
}(...) //<- here you're calling the function `a`, but your function `a` doesn't return anything (`undefined`)
You can solve this adding a semi-colon:
var a = function(){
console.log('AAA');
}; //<- HERE!
(function(){
console.log(a);
})();
Or, you can declare the function a
as Declaration rather than as Expression
Take at look at this Question to understand a little more.
function a(){
console.log('AAA');
}
(function(){
console.log(a);
})();
Actually you build an IIFE. What that means is that:
var a = function(){
console.log('AAA');
}
()
Actually calls the function, and stores its result in a. If you put a function into that function call as an argument, it goes into nowhere as the first function accepts no parameter.
var a = function(){
console.log('AAA');
};
(function(){
console.log(a);
a();
}());