12/29/2009
This is nice little trick that I have first seen the Netflix guys do while back.
(function($, util){
//Createst Namespace Object
if (!window.FNG) {
window.FNG = {}
};
//Createst Namespace Object
if (!window.FNG.widgets) {
window.FNG.widgets = {}
};
//Createst shortuct
var m = window.FNG.widgets;
//Modal Popup class
m.divPopUp = {
create : function(title, content, percentageWidth){
},
destory : function(){
}
};
})(jQuery, FNG.strings.util);
Through the anonymous function, we can have a closure, and by using the function params in the self-invoking mechanism, we can "import" namespace into the closure. This is basically an equvalent of the C# "using System.Text ..." syntax. It also becomes a nice dependancy checking mechanism. If you accidently forget to include your framework file on the page, you will get an error on load indicating that the particular namespace is not present.