我一直在练习具有适当名称间距的 JavaScript 模块模式。所以基本上我声明了命名空间,每个命名空间都封装了某些模块。这是我到目前为止所写的。代码已正确注释。
// namespace has been defined somewhere not to worry that it will be undefined
if (NAMESPACE == null || typeof (NAMESPACE) == 'undefined') {
NAMESPACE = {};
var id = function (id) {
var _all_ids = {};
var persona = {};
var _id = id; // _id is a local variable that stores the argument
var getId = function () { //this function returns the local private variable
return _id;
}
persona.getId = getId;
var _closed = false;
var close = function () {
delete _all_ids[getId()];
this._closed = true;
}
persona.close = close;
return persona; // persona is an object that has two properties `getId` and `close`. Both these are functiona
}
NAMESPACE['id'] = id; // so basically this will become NAMESPACE.id.getId or NAMESPACE.id.close
}
我已经完全注释了这个代码,任何人都可以理解。它声明一个简单的命名空间,然后在其中添加一个模块。该模块当然是使用封装。
我的一位导师建议该代码具有基本的缺陷标准或其他。尽管代码运行良好,但我无法弄清楚这一点。
标准明智吗?还是我做错了?