i want to create a js library (for helping with tests). i want this library to add one new method X
to Array or Object. but some people may not want this behavior and instead would prefer to be able to choose the name (Y
instead of X
) or even having new method at all but use it like X(array).x()
.
some people use requireJs but someone don't. so what are the best practices for providing such new functionality? how this should be done to avoid names collision and to make the library easy to use?
is there any way of providing new functionality without polluting the namespace, in a way to let the user assign it to any variable he wants?
Clarification:
let's say i want to add a method to an array that prints 'hello world'. i can provide a global variable 'HelloWorldLibrary' and then user can bind it to any method he wants. but this is already introducing one name into global namespace. is there any way to avoid it or is that the right way and i should do it this way?