Skip to content Skip to sidebar Skip to footer

How Should I Encapsulate My Library?

I am going to go with this possibly var my_lib = { /* my_code */ } as a way to not clutter the global name space. Is this OK?

Solution 1:

(function (global) {
 /* my code */global["someName"] = someObject;

})(window);

Solution 2:

That's fine, however var will limit its scope. You might also want to wrap it in a closure as well.

Solution 3:

There are different ways to accomplish it. I ran into this article by kangax in the past: http://perfectionkills.com/unnecessarily-comprehensive-look-into-a-rather-insignificant-issue-of-global-objects-creation/

which walks you through different approaches. I think this is the best article on the topic.

Post a Comment for "How Should I Encapsulate My Library?"