Javascript: Why can some functions be related and mapped, while others not?

Out of curiosity:

MDN taught me how to do this:

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

join = Function.prototype.call.bind(Array.prototype.join)

Now I can display trim, but not joinfor any reason. jointakes ','as default argument (delimiter), so I should be fine, but instead it uses an 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 really wanted a different delimiter? Passing it on binddoes not work, because it is added to the existing arguments (at any time, one of the elements of the list on which I map). Then it takes on the role of the strings for concatenation, and the strings for concatenation will act as separators, if there is something to separate:

> joins = Function.prototype.call.bind(Array.prototype.join,';')
call()
> [['a','b'],['c','d']].map(joins)
[";", ";"]

I researched and found:

, this , bind,

, thisArg , MDN map

+4
1

, , 3 . .

Array.prototype.map = function(f) {
  var result = [];
  for(var i = 0; i < this.length; i++) {
    result.push(f(this[i], i, this));
  }
  return result;
}

, [['a','b'],['c','d']].map(join) , map

join(['a', 'b'], 0, [['a','b'],['c','d']])
join(['c', 'd'], 1, [['a','b'],['c','d']])

, , , . .

function joinSep(separator) {
  return function(array) {
    return array.join(separator)
  }
}
var joins = joinSep(';');
[['a','b'],['c','d']].map(joins)
+2

Source: https://habr.com/ru/post/1598955/


All Articles