3 ms·
Be aware there's a mistake in the examples of using bind() as a currying helper. const multiply = (a) => (b) => a * b ... const multiplyByTwo = mul
by ojosilva 5y ago
Be aware there's a mistake in the examples of using bind() as a currying helper.
const multiply = (a) => (b) => a * b
...
const multiplyByTwo = multiply.bind(this, 2)
multiplyByTwo(4); // returns 8
It does not return 8, it returns a function that returns a function that multiplies by 2. So multiplyByTwo()(4) would return 8.
The whole idea of currying with bind() is flawed, as it requires the programmer to think about `this` and can prefill many arguments at once, when currying is, in its essence, about breaking 1 function with N arguments into N functions with 1 argument.
There are more problems with other examples. The use of call() in the following snippet is also questionable:
const calc = {
multiply: function (a, b) { return a * b; },
multiplyMany:
function (...args) { return [].reduce.call(args, this.multiply)}
};
multiplyMany could just be written as `return args.reduce(this.multiply)`. Why set the context twice with the empty array `[]` and then call(args,...)?
The rest of the article has plenty of unnatural or retorted demonstrations of call(), apply() and bind() that are neither illustrative nor educational.