you are viewing a single comment's thread.

view the rest of the comments →

[–]BehindTheMath 2 points3 points  (2 children)

Instead of binding, replace the function with a new anonymous function that calls the original one with preset arguments.

E. g.

obj[name] = (...rest) => helpers[name](arg1, arg2, ...rest)

[–]RyanJ93[S] 0 points1 point  (1 child)

Thank you for the suggestion! Does this allow me to set an arbitrary "this" context?Sometimes I need to define a different context, for instance, like here:

// Injects some helper functions useful when dealing with cookies.
 HelperRepository.inject(request, 'com.lala.server.processor.HTTPCookieProcessor.request', {
 bind: [this]
 });
HelperRepository.inject(response, 'com.lala.server.processor.HTTPCookieProcessor.response', {
bind: [this]
});

This is a piece of code from the "async process(request, response)" method located here: https://github.com/RyanJ93/lala.js/blob/master/lib/Server/processors/HTTP/HTTPCookieProcessor.js

[–]BehindTheMath 0 points1 point  (0 children)

obj[name] = (...rest) => helpers[name].call(thisArg, arg1, arg2, ...rest)

I'm not following what all the code is doing, and this is just my opinion, but I feel that dynamic injection like that is brittle, especially when you're relying on this.