3

I'm building a javascript library and would like to bake in various dependencies (e.g, jQuery, Google Closure, etc) without polluting the global namespace. This is particularly important because others may already be using different versions of these libraries.

As far as I can tell most of these libraries attach themselves directly to the window object, so I cannot simply put them in an anonymous function.

Are there any general solutions to this problem?

1 Answer 1

2

I believe you would need to modify the library source code to be able to do this, replacing their code:

window.jQuery = function(){ ... }

with:

myNamespace.jQuery = function() { ... }

Any any reference to the global jQuery object (and alias) would have to be namespaced, too. Similar measures would have to be taken for each JS library.

In my opinion, this is too much trouble for what it's worth. Taking up a single global variable for each js library is acceptable.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.