WTF: Anonymous function in JavaScript, what about private functions?

Oh I have nodding my head whenever Jeremy Foster when he speaks about anonymous functions in JavaScript.  And that is all I have been doing, now I need to figure out just what the freaking heck is this anonymous function thingie in HTML 5 for Metro/Windows 8 design.  As a C#/C++ programmer I am used to the idea of private functions, in JavaScript there is no such thing and the wrapping of the JavaScript in an anonymous function makes those functions private with in the scope of the anonymous function.

So I started with

The entire file is an anonymous function, the wrap looks like:

(function() {



If you look at the default.js you will see that nomenclature.  So what in the heck is that last set of parentheses?  Did LISP suddenly sneak back into mainstream programming?

No, by making the JavaScript an anonymous function the javascript which protects your global variables for the javascript file a function.  This also prevents the global namespace from having ambiguous names.  JavaScript doesn’t support the idea of a private function and the anonymous wrapping makes it private, a good thing.



Comments (0)

Skip to main content