-
-
Save dworznik/518ed1b943b6a1cf2fe55998e5089c02 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*======================================================================*\ | |
ICBIaW50OiBtYWtlIHRoaXMgYXMgY2xvc2UgdG8gcHJvZHVjdGlvbi1yZWFkeSBzb3VyY2 | |
UgY29kZSBhcyB5b3UgY2FuIQoKICBCb251cyBwb2ludHMgZm9yIHRlbGxpbmcgdXMgd2hh | |
dCB0aGlzIGRvZXMgaW4gcGxhaW4gdGVybXM6CgogICAgJycuam9pbihpdGVydG9vbHMuY2 | |
hhaW4oKnppcChzWy0yOjotMl0sIHNbOjotMl0pKSk= | |
\*======================================================================*/ | |
if (NAMESPACE == null | |
|| typeof (NAMESPACE) == 'undefined') { | |
NAMESPACE = {}; | |
// Creates an object that allocates a new or references an | |
// existing very expensive resource associated with `id` | |
var resource = function (id) { | |
// Private data | |
var _all_ids = new Array(); | |
var _closed = false; | |
var _id = id; | |
var _expensive_resource = null; | |
// Public data | |
var persona = { | |
}; | |
// Public methods | |
var getExpensiveResource = function () { | |
return _expensive_resource; | |
} | |
persona.getExpensiveResource = getExpensiveResource; | |
var getId = function () { | |
return _id; | |
} | |
persona.getId = getId; | |
var close = function () { | |
delete _all_ids[_id]; | |
this._closed = true; | |
} | |
persona.close = close; | |
// Private methods | |
function _lookupOrCreateExpensiveResourceById(id) { | |
_expensive_resource = _all_ids[id]; | |
if (_expensive_resource == null) { | |
// Just pretend for the sake of this example | |
_expensive_resource = { | |
value: "I'm a very expensive resource associated with ID " + id | |
}; | |
_all_ids[id] = _expensive_resource; | |
} | |
return _expensive_resource; | |
} | |
// Initialization | |
_expensive_resource = _lookupOrCreateExpensiveResourceById(id); | |
return persona; | |
} | |
NAMESPACE.resource = resource; | |
} |
// First of all, I tested this code and in general, it works. I found the following error in NAMESPACE == null || typeof (NAMESPACE) == 'undefined'
should be typeof (NAMESPACE) === 'undefined' || NAMESPACE === null
in case NAMESPACE isn't defined it will error out instead of not entering the if block
// With that in mind, I think that the main problem with this code is that it is never deleting the expensive resource. The close function does not delete the resource from memory, unless that is done intentionally, it's bad practice and allocates too many resources.
No comment on this code.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
/======================================================================
ICBIaW50OiBtYWtlIHRoaXMgYXMgY2xvc2UgdG8gcHJvZHVjdGlvbi1yZWFkeSBzb3VyY2
UgY29kZSBhcyB5b3UgY2FuIQoKICBCb251cyBwb2ludHMgZm9yIHRlbGxpbmcgdXMgd2hh
dCB0aGlzIGRvZXMgaW4gcGxhaW4gdGVybXM6CgogICAgJycuam9pbihpdGVydG9vbHMuY2
hhaW4oKnppcChzWy0yOjotMl0sIHNbOjotMl0pKSk=
*======================================================================*/
var NAMESPACE;
if (NAMESPACE == null || typeof(NAMESPACE) == 'undefined') {
}