Anonymous functions are functions declared without a name.
For example (using jQuery):
$.each(array, function(i,v){ alert(v); });
The function here is anonymous, it is created specifically for this call to $.each .
Closing is a type of function (it can be used in an anonymous function or it can be called), where the parameters passed to it are โcapturedโ and remain unchanged even outside the scope.
Closing (in JavaScript):
function alertNum(a){ return function(){ alert(a); } }
Closing returns an anonymous function, but it should not be an anonymous function.
Continuing the closure example:
alertOne = alertNum(1); alertTwo = alertNum(2);
alertOne and alertTwo are functions that will trigger warnings 1 and 2, respectively, when called.
Rocket Hazmat Dec 21 '10 at 16:41 2010-12-21 16:41
source share