user656925
user656925

Reputation:

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?

Upvotes: 0

Views: 965

Answers (3)

Michal Kuklis
Michal Kuklis

Reputation: 911

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.

Upvotes: 0

Raynos
Raynos

Reputation: 169391

(function (global) {
 /* my code */

 global["someName"] = someObject;

})(window);

Upvotes: 1

Daniel A. White
Daniel A. White

Reputation: 190945

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

Upvotes: 0

Related Questions