在编写JavaScript类和命名空间时是否有任何好处?
if(typeof MyNamespace === 'undefined'){
var MyNamespace = {};
}
(function(){
MyNamespace.MyClass = function(){
this.property = 'foo'
return this;
}
}());
与仅有这个相比...
if(typeof MyNamespace === 'undefined'){
var MyNamespace = {};
}
MyNamespace.MyClass = function(){
this.property = 'foo'
return this;
}
我看到第一种模式在几个库中都得到了实现,但是我想知道除非在第一个示例的匿名函数内声明某种其他函数,否则是否有任何附加好处。