Array.prototype.method.call - what is the purpose?

Hi i wonder if someone can explain Array.prototype.reduce.call

is it:

  1. convert to Array
  2. invoke it in array format (call)?

Or am i missing something?

thanks

//countLetters function that is called twice (for the word and the currentword)

  const countLetters = word =>

    Array.prototype.reduce.call(

      word,

      (counts, letter) => {

        letter = letter.toLowerCase();

        counts[letter] = counts[letter] || 0;

        counts[letter]++;

        return counts;

      },

      {}

    );

  // k

  const looseEqual = (obj1, obj2) => {

    const obj1Keys = Object.keys(obj1);

    const obj2Keys = Object.keys(obj2);

    if (obj1Keys.length != obj2Keys.length) {

      return false;

    }

    return obj1Keys.every(key => obj1[key] === obj2[key]);

  };

  //looseEqual(letterCount, countLetters(currentword)):

  //countLetters(currentWord) will be called first since its within the function

  function anagrams(word, words) {

    const letterCount = countLetters(word);

    return words.filter(currentWord =>

      looseEqual(letterCount, countLetters(currentWord))

    );

}

anagrams("abba", ["aabb", "abcd", "bbaa", "dada"]);

Just to call a function. You pass the arguments same as you would pass them to the function. And apply is exactly the same but you pass the arguments as an array.

function add (a, b) {
  return a + b;
}

All the same:

add(2, 2);
add.call(null, 2, 2);
add.apply(null, [2, 2]);

Sometimes you need to call a function dynamically, or you need to borrow a function from an object and/or use it in different scope (that is what the first argument is for, it sets what the value of this is). In the example, reduce comes from the Array object, and is being borrowed for use on the String object.