I have been practicing with JavaScript module pattern with proper Name-spacing. So basically I declare namespaces and each namespace has certain modules encapsulated in it. Here is what I have written so far. The code has been commented properly.
// 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
}
I have fully commented tis code for anyone to understand. It declares a simple namespace and then adds a module in it. The module of course is using encapsulation.
One of my instructors suggest that this code has a basic flaw standard or otherwise. I can't figure this out although the code runs fine.
Is it good enough standard wise? Or am I doing it totally wrong?