2
我想在某些命名空间中保留obj consructors,并且在调试时没有问题。在命名空间中定义obj构造函数的最佳方法javascript
现在我有这样的代码:
var namespace = {};
namespace.myConstructor = function(){};
// ----------- debug in console
(new namespace.myConstructor()); // -> namespace.myConstructor {}
(new namespace.myConstructor()).constructor; // -> function(){}
我不喜欢这个构造是匿名的。 所以我能做到这一点在其他方面:
(更好,但丑陋的)
var namespace = {};
namespace.myConstructor = (function(){
function myConstructor(){};
return myConstructor;
})();
// ----------- debug in console
(new namespace.myConstructor()); // -> myConstructor {}
(new namespace.myConstructor()).constructor; // -> function myConstructor(){}
或(最beautful和最短路径)
namespace.myConstructor = function myConstructor(){};
// ----------- debug in console
(new namespace.myConstructor()); // -> myConstructor {}
(new namespace.myConstructor()).constructor; // -> function myConstructor(){}
但我读here,有是NFE(命名函数表达式)的一些问题。
哪种方式更好?好的做法是哪种方式?
@我已经删除了额外的问题。现在好吗? – akaRem