How to call a function with a function as parameter?

Bala

As a beginner in Javascript, I am trying to call a function whose parameter is a function.But I get this error.

function execute(f) {
  f();
}

var m = function(x) { return x * x };
m(8)                          #=> returns 64    
execute(m(8))                 #=>  Uncaught TypeError: number is not a function

I guess function execute is executing m(8) before exercising its body, therefore the call becomes execute(64). If so, how do I go about passing a function with a parameter?

Denys Séguret

You'll have to pass them as separate parameters and use call or apply.

For example :

function execute(f, args) {
   return f.apply(null, args);
}

var m = function(x) { return x * x };
m(8)                            
execute(m, [8])

Now, let's suppose you want to have functions taking any number of arguments, and you don't want to bother with making an array. Then it's a little more fun :

function execute() {
  return arguments[0].apply(null, [].slice.call(arguments, 1));
}

var m = function(x,y) { return x * y };
console.log(m(8, 4))                  
console.log(execute(m, 8, 4))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to call a JavaScript function with parameter in jQuery?

From Dev

How to call one function with different parameter types

From Dev

How to call a JavaScript function with parameter in jQuery?

From Dev

How to call class template function with parameter

From Dev

How to call a function with a parameter as string in php?

From Dev

How to call DLL function using "&" parameter?

From Dev

How can I call function with parameter in bash?

From Dev

How to properly call a function with date parameter?

From Dev

how to call function where parameter based on a promise

From Dev

How to call on init function from textarea with parameter?

From Dev

How to call a parameter list from a function in Julia

From Dev

Function call as parameter value?

From Dev

Function as method call parameter

From Dev

Call a function, with array parameter

From Java

How do I disambiguate this call to a generic function with a function parameter in Swift?

From Dev

How to call a function another function as a parameter passed as a string

From Dev

AngularJS ng-click: how to call a function with a function as parameter?

From Dev

How to call the JSP function and send the parameter using the function

From Dev

How to call the JSP function and send the parameter using the function

From Dev

how to call a function which is passed as parameter in another function

From Dev

How to call a Scala function with a single parameter and write the parameter first

From Dev

javascript anonymous function call with parameter

From Dev

Create a vector as a function call parameter

From Dev

Call a function that requires a KeyEvent parameter

From Dev

Call a parameter of a function in another one

From Dev

AJAX call with $(this) parameter in SUCCESS function

From Dev

Wierd ambiguious call on overloaded function with a function parameter

From Dev

Javascript function call with another function as parameter

From Dev

Is it possible to call a function as a parameter of another function in Swift?

Related Related

  1. 1

    How to call a JavaScript function with parameter in jQuery?

  2. 2

    How to call one function with different parameter types

  3. 3

    How to call a JavaScript function with parameter in jQuery?

  4. 4

    How to call class template function with parameter

  5. 5

    How to call a function with a parameter as string in php?

  6. 6

    How to call DLL function using "&" parameter?

  7. 7

    How can I call function with parameter in bash?

  8. 8

    How to properly call a function with date parameter?

  9. 9

    how to call function where parameter based on a promise

  10. 10

    How to call on init function from textarea with parameter?

  11. 11

    How to call a parameter list from a function in Julia

  12. 12

    Function call as parameter value?

  13. 13

    Function as method call parameter

  14. 14

    Call a function, with array parameter

  15. 15

    How do I disambiguate this call to a generic function with a function parameter in Swift?

  16. 16

    How to call a function another function as a parameter passed as a string

  17. 17

    AngularJS ng-click: how to call a function with a function as parameter?

  18. 18

    How to call the JSP function and send the parameter using the function

  19. 19

    How to call the JSP function and send the parameter using the function

  20. 20

    how to call a function which is passed as parameter in another function

  21. 21

    How to call a Scala function with a single parameter and write the parameter first

  22. 22

    javascript anonymous function call with parameter

  23. 23

    Create a vector as a function call parameter

  24. 24

    Call a function that requires a KeyEvent parameter

  25. 25

    Call a parameter of a function in another one

  26. 26

    AJAX call with $(this) parameter in SUCCESS function

  27. 27

    Wierd ambiguious call on overloaded function with a function parameter

  28. 28

    Javascript function call with another function as parameter

  29. 29

    Is it possible to call a function as a parameter of another function in Swift?

HotTag

Archive