Skip navigation

/*Anonymous function with parameter, executed immediately*/
(function(foo, bar){
alert(foo*bar);
})(2,3);

Interestingly anonymous functions used to create closures. Closure is protected variable space.

Consider the following function
(function(){
var foo=2;
var bar=3;
mul = function(){
alert(foo*bar);
}
})();

You won’t get any alerts after its execution. but
mul();

will give an alert !

After anonymous function’s execution, how can function mul get executed? that too accessing its foo and bar?

Will update the post soon!!!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: