I'm very new to programming and have a quick question regarding a demonstration of closure given here: http://youtu.be/hQVTIJBZook?t=27m19s.
The code is:
var digit_name = function () {
var names = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'];
return function (n) {
return names[n];
};
} ();
alert(digit_name(3));
My question is: Why is there an additional ();
outside of the function digit_name
? Are we calling the anonymous secondary function? I ran it without the ();
and it came back with " function (n) {return names[n];} "
. I would really appreciate a description of what happens to the parameter 3 when you pass it to the function digit_name, since that function doesn't have a specified parameter.
I apologize if I'm asking a basic question or using incorrect terminology. I've looked into a few related questions but alas, to no avail! Thank you in advance to those kind enough to provide a well-fashioned answer.