0

I would like to execute all functions in an array in global scope. I want to use a shorthand so I decided to use Function.prototype.call and call Function.prototype's call method on all functions. I don't care about the arguments forEach sends to the functions.

[function(){}, function(){}].forEach(Function.prototype.call.call)

Why this is not working and throwing following error?

TypeError: undefined is not a function
Mohsen
  • 64,437
  • 34
  • 159
  • 186

2 Answers2

5

You want:

[function(){}, function(){}]
  .forEach(Function.prototype.call.bind(Function.prototype.call))

But you can simplify a bit using the Function constructor which already gives you call:

.forEach(Function.call.bind(Function.call))
elclanrs
  • 92,861
  • 21
  • 134
  • 171
0

Why this is not working and throwing following error?

The internal call looks something similar to

Function.prototype.call.call.apply(undefined, function() {}, 0, [])
                                     ^--- it's `undefined` here because you haven't
                                          specified anything explicitly
                                          as a 2nd `forEach` arg

And internally call expects that its this will point to the correct function (Function.prototype.call in this case), whereas "you" are passing undefined

So technically this code works as expected:

[
    function(){console.log(1)},
    function(){console.log(2)}
]
    .forEach(Function.prototype.call.call, Function.prototype.call)

But the elclanrs' solution with bind is better

zerkms
  • 249,484
  • 69
  • 436
  • 539
  • Oh, I was adding this to my answer from your comments, I'll upvote then. – elclanrs Sep 25 '14 at 22:14
  • [`Function` can be overriden, in that case your code won't work. -1](http://stackoverflow.com/a/17245542/633183) – Mulan Sep 28 '14 at 21:44
  • @naomik: I **NEVER EVER** mentioned my solutions are ideal and take into account everything. Stop being a kid. – zerkms Sep 28 '14 at 23:14