Skip to content

Instantly share code, notes, and snippets.

@svapreddy
Created May 13, 2016 11:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save svapreddy/41a70efd54ba991cf21c248da9acec1f to your computer and use it in GitHub Desktop.
Save svapreddy/41a70efd54ba991cf21c248da9acec1f to your computer and use it in GitHub Desktop.
Problem: https://gist.github.com/timoxley/a34546c28c92025d040a391400ba5eb7
Solution: fns.forEach([].forEach.bind([1]))
Explanation: [].forEach.bind(someArray) will return a forEach function binded with passed `someArray`. So we can use this as iterator for `fns.forEach`. But the problem is fns.forEach will be executed `fns.length` times on `[].forEach.bind(someArray)` So each function inside fns will be called fns.length times.
fns.forEach.call(fns, [].forEach.call(fns)) // Will execute each function fns.length times
fns.forEach.call(fns, [].forEach.call([1,2])) // Will execute each function 2 times
fns.forEach.call(fns, [].forEach.call([1])) // Will execute each function 1 time
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment