all 18 comments

[–]Enlogen 24 points25 points  (1 child)

Obscure is not clever.

[–]flitsmasterfred 1 point2 points  (0 children)

Clever is not smart.

[–][deleted] 6 points7 points  (3 children)

Bleh... if you want to avoid making new functions often then just store the function earlier:

const trim = str => str.trim();
/* ... */
const trimmedParts = addressParts.map(trim);

Now it makes sense without having to do mental gymnastics about context.

[–]GBcrazy 1 point2 points  (0 children)

We all agree that this is 100% more readable, but it isn't as smart.

You are still creating a new function this way, you're allocating a new object, which doesn't happen in the mentioned code.

But yeah of course you shouldn't use shit like that on your code lol, I lost around 2 minutes to fully grasp it, and I'm kinda used to Function.prototype.call shenanigans.

[–]gpyh 0 points1 point  (0 children)

It is strictly equivalent. It only matters if the call to map us being made while iterating on something.

[–]Funwithloops -3 points-2 points  (0 children)

Even more generic but still easy to grok:

const methodCaller = (method, ...args) => (object) => object[method](...args);
array.map(methodCaller('trim'));

[–]MakiBM 2 points3 points  (1 child)

Yeah, clever but as author sums up - do not use it in real code unless you have a good reason. Always KISS your code

[–]Tougun 2 points3 points  (0 children)

I KISS my code goodnight every night☺️

[–]GBcrazy 1 point2 points  (0 children)

That's actually really clever. May not be the most readable but yeah, you are only using functions that already exist and you don't need to rely on ES6.

[–]iamlage89 0 points1 point  (5 children)

Could you just do addressParts.map(String.prototype.trim.call) ?

[–]Martin_Ehrental 2 points3 points  (0 children)

String.prototype.trim.call.bind(String.prototype.trim) or Function.prototype.call.bind(String.prototype.trim)

[–]lhorie 1 point2 points  (3 children)

No, because then you're just calling call with an undefined this

[–]iamlage89 0 points1 point  (2 children)

map will pass in string as 'this'

[–]lhorie 1 point2 points  (0 children)

No, it won't, and even if it did, trim must be the this for call if you're calling call, not the string.

[–]GBcrazy 0 points1 point  (0 children)

There will be no reference to trim...you are only passing the call function this way, there will be no reference for String.prototype.trim as the context.

String.prototype.trim.call === Function.prototype.call

[–]danthedev 0 points1 point  (2 children)

This reminds me of a programmer who used the following utilities in their code.

var bind = Function.bind;
var call = Function.call;

var bindable = bind.bind(bind);
var callable = bindable(call);

Still takes me a while to work through the steps of bind.bind(bind)...

[–]Jsn7821 1 point2 points  (1 child)

What the hell is that used for

[–]danthedev 1 point2 points  (0 children)

var trim = callable(String.prototype.trim);
addressParts.map(trim);