Using the anonymous function of JavaScript
The Anonymous function of JavaScript is very useful for avoiding conflicts in the JavaScript library.
How to do it...
Let's first understand the Anonymous function with an example:
(function(msg) { alert(msg); }) ('Hello world');
When we execute the preceding code in a browser, it will display the alert Hello world
. Now, that's interesting! A function is defined and also executed! Let's simplify the same code snippet and see how it works:
Var t = function(msg){ alert(msg); }; t('Hello world');
If you see the equivalent code, it's straightforward. The only difference is that this simplified code has the variable name t
associated with the function while, in the other code, the function name is anonymous. The Anonymous function gets executed instantly after declaring it.
Anonymous functions are very useful while creating the plugins of JavaScript frameworks, as you don't have to worry about conflicts with other plugins that have the same name as...