Javascript 在javascript函数中将函数作为参数传递

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5752030/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 18:42:34  来源:igfitidea点击:

Passing a function as an argument in a javascript function

javascript

提问by Spence

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

我想知道这样做是否合法。我可以有类似的东西:

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

where ais an array and xis an integer argument for another function called foo?

其中a是一个数组,x是另一个名为 的函数的整数参数foo

(The idea is to have one function that uses a for loop on the array, and calls that function in the params for every element in the array. The idea is so call this on different functions so elements of two arrays are multiplied and then the sums are added together. For example A[0] * B[0] + A[1] * B[1].)

(这个想法是让一个函数在数组上使用 for 循环,并在数组中的每个元素的 params 中调用该函数。这个想法是在不同的函数上调用它,这样两个数组的元素相乘,然后总和加在一起。例如A[0] * B[0] + A[1] * B[1]。)

回答by Sergei Golos

I think this is what you meant.

我想这就是你的意思。

funct("z", function (x) { return x; });

function funct(a, foo){
   foo(a) // this will return a

}

回答by KooiInc

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 functis a referenceto 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).

因此, 的第二个参数functreference另一个函数(在本例中为foodemo)。一旦函数被调用,它就会执行另一个函数(在这种情况下使用第一个参数作为它的输入)。

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

函数声明中的参数只是标签。是函数体赋予它们意义。funct如果未提供第二个参数,则在此示例中将失败。所以检查它可能看起来像:

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 canuse a direct function call as parameter within a function call (with the right function definition):

由于 JS 的性质,您可以在函数调用中使用直接函数调用作为参数(使用正确的函数定义):

function funct2(foo){
  alert(foo);
}

funct2(foodemo('world!')); //=> 'hello world!'

回答by dallin

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 applyfunction and have a third parameter that is an array of the arguments, like such:

如果您愿意,也可以使用apply函数并使用第三个参数作为参数数组,例如:

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"]); 

回答by Rajkumar

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:

在 removeETC 方法中执行如下:

function removeETC(delAction){ delAction(); }

回答by Prado

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

你说的都是合法的。在这里,foo(X)将被调用,其返回值将作为funct()方法的参数

回答by julx

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) { ... }).

如果要将另一个调用结果传递给函数,只需编写funct(some_array, foo(x)). 如果你想传递另一个函数本身,那么写funct(some_array, foo). 你甚至可以传递一个所谓的匿名函数funct(some_array, function(x) { ... })

回答by Hakk? Eser

In fact, seems like a bit complicated, is not.

其实好像有点复杂,其实不然。

get method as a parameter:

get 方法作为参数:

 function JS_method(_callBack) { 

           _callBack("called");  

        }

You can give as a parameter method:

您可以将其作为参数方法提供:

    JS_method(function (d) {
           //Finally this will work.
           alert(d)
    });