Closed cliffordfajardo closed 8 years ago
forEach
is a higher order function whose argument function takes other arguments as well.
It takes each element, its index, and entire array. Please extend your implementation to handle those as well.
I like the pointfree style you are using, but if you are really implementing it for your own usage (say, polyfill), go all the way. Use this syntax:
Array.prototype.forEach = Array.prototype.forEach || function(element, index, array){
/* implementation with for loop */
}
We should also mention somewhere that this is not what the real implementation is. The real implementation, at least in V8, is much more complex. This might interest you.
@alayek - I was also thinking about showing how to add it to the Array prototype, but for beginners who don't understand what prototype is that would be confusing. I'll update this example & the other pull requests to add to the array prototype. I'll also include a description that this is a pseudo implementation of how it works. Thanks for the link
added this section for people who are curious about how forEach works behind the scenes. It's not magic, it's javascript =).