Passing a function as an argument in a javascript function

后端 未结 7 1127
眼角桃花
眼角桃花 2020-12-08 00:25

I was wondering whether this is legal to do. Could I have something like:

function funct(a, foo(x)) {
  ...
}

where a is an ar

相关标签:
7条回答
  • This is not the way to declare a function with another function as one of it's parameters. This is:

    function foodemo(value){
      return 'hello '+ value;
    }
    
    function funct(a, foo) {
      alert(foo(a));
    }
    
    //call funct
    funct('world!', foodemo); //=> 'hello world!'
    

    So, the second parameter of funct is a reference to another function (in this case foodemo). Once the function is called, it executes that other function (in this case using the first parameter as input for it).

    The parameters in a function declaration are just labels. It is the function body that gives them meaning. In this example funct will fail if the second parameter wasn't provided. So checking for that could look like:

    function funct(a, foo) {
      if (a && foo && typeof a === 'string' && typeof foo === 'function'){
        alert(foo(a));
      } else {
        return false;
      }
    }
    

    Due to the nature of JS, you can use a direct function call as parameter within a function call (with the right function definition):

    function funct2(foo){
      alert(foo);
    }
    
    funct2(foodemo('world!')); //=> 'hello world!'
    
    0 讨论(0)
  • 2020-12-08 01:07

    If you want to pass a function, just reference it by name without the parentheses:

    function funct(a, foo) {
       ...
    }
    

    But sometimes you might want to pass a function with arguments included, but not have it called until the callback is invoked. To do this, when calling it, just wrap it in an anonymous function, like this:

    funct(a, function(){foo(x)});
    

    If you prefer, you could also use the apply function and have a third parameter that is an array of the arguments, like such:

    function myFunc(myArray, callback, args)
    {
        //do stuff with myArray
        //...
        //execute callback when finished
        callback.apply(this, args);
    }
    
    function eat(food1, food2)
    {
        alert("I like to eat " + food1 + " and " + food2 );
    }
    
    //will alert "I like to eat pickles and peanut butter"
    myFunc([], eat, ["pickles", "peanut butter"]); 
    
    0 讨论(0)
  • 2020-12-08 01:14

    And what would you like it to achieve? It seems you mixed up a function declaration with a function call.

    If you want to pass another calls result to a function just write funct(some_array, foo(x)). If you want to pass another function itself, then write funct(some_array, foo). You can even pass a so-called anonymous function funct(some_array, function(x) { ... }).

    0 讨论(0)
  • 2020-12-08 01:20

    What you have mentioned is legal. Here, foo(X) will get called and its returned value will be served as a parameter to the funct() method

    0 讨论(0)
  • 2020-12-08 01:25

    I would rather suggest to create variable like below:

    var deleteAction = function () { removeABC(); };
    

    and pass it as an argument like below:

    removeETC(deleteAction);
    

    in removeETC method execute this like below:

    function removeETC(delAction){ delAction(); }
    
    0 讨论(0)
  • 2020-12-08 01:27

    I think this is what you meant.

    funct("z", function (x) { return x; });
    
    function funct(a, foo){
       foo(a) // this will return a
    
    }
    
    0 讨论(0)
提交回复
热议问题