Take a look at this piece of code:
var array = [0, 1];
var even = function(element, index, array, considerZeroEven) {
// checks whether an element is even
if (element === 0)
return considerZeroEven;
return element % 2 === 0;
};
console.log(array.some(even)); // Here I should do the trick...
Is it possible to pass to even the considerZeroEven parameter?
I saw I should use thisArg and do something dirty inside there, but I do not think this is a good idea, and looking at the polyfill on MDN it looks like it will pass only the value of the element, the index of the element, and the array object being traversed, so nothing more could be done.
You could take a closure over the wanted value of considerZeroEven
and take just a function as callback without using thisArg
.
var array = [0, 1],
even = function (considerZeroEven) {
return function(element, index, array) {
return element === 0
? considerZeroEven
: element % 2 === 0;
};
};
console.log(array.some(even(false)));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With