-1

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?

piotrek
  • 13,982
  • 13
  • 79
  • 165
  • I dont get it...you mean like overriding the name but with the same functionality? – Typo Apr 26 '15 at 21:09
  • no. i want to provide completely new functionality with new name. but i feel that choosing the name arbitrary may not be the best solution. so is there any way to let the user customize the name? – piotrek Apr 26 '15 at 21:10
  • 1
    make it an object itself, and let the user name it as he/she wants – Typo Apr 26 '15 at 21:14
  • but how can i create a js file that return an object without a name? can you write some code? – piotrek Apr 26 '15 at 21:16
  • var a = new Object(); and then x(array).x = a; – Typo Apr 26 '15 at 21:17
  • or var a = new Object(); and then x(array).a = a; – Typo Apr 26 '15 at 21:18
  • but `new Object()` doesn't allow me to provide my functionality. it would have to be `new MyLibrary()` and therefore i introduce unnecessary name in the global namespace. is there any way to avoid it? – piotrek Apr 26 '15 at 21:20
  • General rule of thumb - if you don't own it, don't change it. – garryp Apr 26 '15 at 21:28

1 Answers1

0

No, there is no way to currently bind a method to an object in a scoped way, the justification is that it can be accomplished by external methods, using custom objects (rather than directly sticking it on Array or Object) and that it's expensive at runtime.

The use case is very real though (adding a property for every object), there was an abstract references proposal that did exactly this - it's now superseded by the function bind proposal, the proposed syntax for ES7 looks something like this:

function myNewMethod(x){
    alert(this.y + x);
}

var obj = {y: 5};
obj::myNewMethod(5); // logs 10

As for actual module management - you can use ES6 modules with the module loader API. ES6 modules (the import and export keywords) allow you to import/export stuff without polluting the global namespace. Solutions for systems prior to ES6 exist using module loaders like RequireJS and tools like browserify.

Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504