I’m building a library (https://github.com/OscarGodson/storageLocker), a localStorage wrapper to be more exact, but because this is my first try at OO JavaScript I’m still learning and I have a couple questions.
-
I’ve seen in other libraries that sometimes they wrap them in a anonymous function. Do I, or should I, do that with this? And if so, how without breaking anything?
-
For the internal API (basically, the internal functions) how should I write them? Should add them to the main object e.g.
storageLocker.prototype.myInternalFunction()or justmyInternalFunction()randomly in my script? I didn’t want the functions to be global though… One of the functions for example just checks a bunch of items in the JSON, sees if their objects, and then checks what the object type is (likeDate()) and then converts it. -
How/where should I add global, to my script, vars? e.g. i have a var called
patternsthat is something likevar patterns = {"date":/\/Date\(([0-9]+)\)\//}how should I add that into my script?
Thanks a lot. I want to write my script the right way so im asking you guys. I don’t know of any JS guys locally that do any OO JS, they’re all old school types…
has a decent section on namespacing worth reading.
is also another good overview.
For more great material on good javascript practices, check out
After our discussion in the comments, I’ve changed the example to this:
While this isn’t exactly an example of a library/module (because you’re accessing the code by calling the constructor directly), it is an example of keeping the same object in scope for further chaining of methods. Actually it’s not returning the
storageLockerobject, it’s returning the ‘public’ object, but that object has access to thestorageLocker‘s scope through closure.There could be other better ways to do this by perhaps returning the
storageLockerobject itself, but that would require a bit more thinking through.