Out of curiosity:
The MDN taught me how to shortcut function application like this:
trim = Function.prototype.call.bind(String.prototype.trim)
join = Function.prototype.call.bind(Array.prototype.join)
Now, I can map trim
, but not join
for some reason. join
takes ','
as default argument (separator), so I should be fine, but instead it uses the array index:
> trim = Function.prototype.call.bind(String.prototype.trim)
call()
> [' a','b '].map(trim)
["a", "b"]
> join = Function.prototype.call.bind(Array.prototype.join)
call()
> [['a','b'],['c','d']].map(join)
["a0b", "c1d"]
Why?
Also, what if I actually wanted a different separator? Passing it to bind
doesn't work, since it is prepended to the existing arguments (at any time one of the elements of the list on which I map). Then it takes the role of the strings to join and the strings to join would act as separators if there was something to separate:
> joins = Function.prototype.call.bind(Array.prototype.join,';')
call()
> [['a','b'],['c','d']].map(joins)
[";", ";"]
I have researched and found:
Answers explaining the thing with this
and solutions equivalent to the bind
shortcut I referenced
Similar explanations, the solution using thisArg
again, from MDN's page about map