temelm
temelm

Reputation: 788

How to import lodash directly into a custom namespace in JavaScript

I would like to import lodash core v4.14.1 from https://raw.githubusercontent.com/lodash/lodash/4.14.1/dist/lodash.core.js. However, simply executing the link adds the library to the global namespace, i.e., window. How can I add the library directly into a custom namespace, say, window.myns? Please note that I don't want to add the library to the global namespace and make a reference to it after.

Upvotes: 2

Views: 746

Answers (2)

Ori Drori
Ori Drori

Reputation: 191976

Use lodash's _.noConflict() to free _ and assign it to a new alias:

window.myns = _.noConflict();

Upvotes: 5

Sebastian Sebald
Sebastian Sebald

Reputation: 16856

If you want to use exactly the linked version of lodash it will always register into window, because that's the way it was build (you can seed this if you scrool to the bottom of the implementation, there is the "export" mechanism).

Guess you have to use some sort of module loader if you want to put it into a namespace. It would be interesting why you want to put it in its own namespace. Because window._ and window.namespace._ is pretty much the same.

Upvotes: 1

Related Questions