I am currently trying to learn Javascript and understand that functions are one of the most important aspects of the language but I have to say Im finding it very hard to understand them especially when different parameters are being used here there and everywhere.
I have been looking at this code which I read in another stack overflow post regarding closure and cant understand how 16 is alerted, I have used console.log to work out what value is being used at each point and seem to get a total of 17, could someone possibly explain?
function foo(x) {
// console.log(x); = 2
var tmp = 3;
return function (y) {
// console.log(y); = 10
alert(x + y + (++tmp));
// console.log(++tmp); = 5?
}
}
var bar = foo(2);
bar(10);
If anyone can offer any wisdom about functions that might make things start to make sense for me it would be really appreciated.
Kyle