Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.
Sign upWhy forEach callback's this is window? #1213
Comments
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
claudepache
Jun 4, 2018
Contributor
I don't think it's wise.
Neither do I, but it is what you get when a function is called without this-value, and forEach is not different from other methods that existed since the dawn of JavaScript.
If you want a wiser behaviour, use strict mode: you’ll get undefined (or null).
(And if you want a reference to the array, use the third parameter of the callback.)
Neither do I, but it is what you get when a function is called without this-value, and If you want a wiser behaviour, use strict mode: you’ll get (And if you want a reference to the array, use the third parameter of the callback.) |
BlackHole1 commentedJun 4, 2018
Whether the second parameters are
emptyornullorundefined, thethisresult iswindow.I don't think it's wise. I think it should be like this:
Here's my Polyfill: