Javascript namespacing convention

wI'm unsure which is the better namespace convention to use.

var App = {}; // global variable, the root of our namespace
(function() {

   App.something = function() {

   }

})();

or

(function() {

   window.App = {}; //global variable, the root of our namespace

   App.something = function() {

   }

})();

window.App and var App are both global variables so both conventions achieve the same outcome, but which is better?


The only difference is that in the first variant, App cannot be deleted from window , although it's accessible as a property of the global object. In the second case, delete window.App works. Also, note that you should be attaching your namespace to window , not Window , as JavaScript is case-sensitive, and Window is a constructor.

Other than that, both are basically the same, there is no "better".

链接地址: http://www.djcxy.com/p/14750.html

上一篇: 饼图点击问题

下一篇: Javascript命名空间约定