var theSet = ["Carel Haverbeke", "Maria van Brussel", "Donald Duck"]; function isInSet(set, person) { return set.indexOf(person.name) > -1; } console.log(ancestry.filter(function(person) { return isInSet(theSet, person); })); console.log(ancestry.filter(isInSet.bind(null, theSet))); 

The first console.log is clear. And the second one with bind ... at first I didn’t understand why the person is not transmitted anywhere, because he is supposed to be undefined. After all, filter takes a function whose argument is equal to each individual element of the array being searched. And then just theSet.

I understand correctly that with such a record a function is returned, but is the first argument of theSet pre-registered in it? That is, you get the normal filter filter (function (person) {return set.indexOf (person.name)> -1;}), and the set argument as if in memory, so to speak? I just want to make sure I understand how the second call option works.

    1 answer 1

    Yes, you understand correctly.

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_objects/Function/bind

     fun.bind(thisArg[, arg1[, arg2[, ...]]]) 

    arg1, arg2, ... - invoking the target function.

    arg1, arg2, ... - parameters that will be passed to the fun function, before those that will be passed to the function returned by fun.bind , with the code that will call this function