Javascript. Код работает без указания его значения this - PullRequest
0 голосов
/ 04 августа 2020

if (!Array.prototype.mapUsingReduce) {
  Array.prototype.mapUsingReduce = function(callback, thisArg) {
    return this.reduce(function(mappedArray, currentValue, index, array) {
      mappedArray[index] = callback.call(thisArg, currentValue, index, array)
      return mappedArray
    }, [])
  }
}

const output=[1, 2, , 3].mapUsingReduce(
  (currentValue, index, array) => currentValue + index + array.length); // [5, 7, , 10]

console.log(output);

Здесь

const output=[1, 2, , 3].mapUsingReduce(
  (currentValue, index, array) => currentValue + index + array.length);

Второй аргумент thisArg не передается.

И

mappedArray[index] = callback.call(thisArg, currentValue, index, array)

Здесь в функции вызова нам нужно thisArg. Так как же вызов по-прежнему работает без указания thisArg?

Я понимаю, что у стрелочных функций нет this.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...