I was trying to pass the 'push' method of array directly to forEach invocation on another array:
result = []
l1 = [1]
f = result.push.bind(result)
l1.forEach(f)
And the result ends up:
> result
[ 1, 0, [ 1 ] ]
If I do, instead:
l1.forEach(function (x) { f(x); })
Then everything works fine. What is going on?
To understand what is going on run this code snipped
[1].forEach(function() {
console.log(arguments);
});
And you'll receive
[1, 0, Array[1]]
Function, supplied to forEach method is called for each array element with the following arguments:
So, it seems like you can't do what you want with binding a push call to specific array instance...
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