这段代码主要帮助你理解在javascript中如何使用命名空间
// namespaced function var NAMESPACED = function (param){ var _privateVar = '_privateMethod loaded: '; var _privateMethod = function (subparam){ var cSubparam = (subparam) ? subparam : ''; alert(_privateVar + param + cSubparam); }(' during declaration'); var _public = { publicVar : _privateVar, publicVar2 : 'public Method', publicMethod : function(subparam){ var cSubparam = (subparam) ? subparam : ''; alert(this.publicVar2 + ': ' + param + cSubparam); }, publicMethod2 : function(){ // ... } }; return _public; }('ok'); window.onload = function (){ NAMESPACED.publicMethod(' on load'); }