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

2 Answers 2

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))
Sign up to request clarification or add additional context in comments.

1 Comment

aside: Function.call === Function.prototype.call; (since inheritance works)
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

3 Comments

Oh, I was adding this to my answer from your comments, I'll upvote then.
@naomik: I NEVER EVER mentioned my solutions are ideal and take into account everything. Stop being a kid.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.