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

时间:2022-01-15 20:08:03

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

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

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

where a is an array and x is 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]。)

7 个解决方案

#1


41  

I think this is what you meant.

我想这就是你的意思。

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

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

}

#2


24  

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

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

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:

函数声明中的参数只是标签。它是赋予它们意义的功能体。在此示例中,如果未提供第二个参数,则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 can use 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!'

#3


7  

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:

如果您愿意,还可以使用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"]); 

#4


2  

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(); }

#5


2  

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()方法的参数

#6


1  

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

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

#7


-1  

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)
    });

#1


41  

I think this is what you meant.

我想这就是你的意思。

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

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

}

#2


24  

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

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

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:

函数声明中的参数只是标签。它是赋予它们意义的功能体。在此示例中,如果未提供第二个参数,则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 can use 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!'

#3


7  

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:

如果您愿意,还可以使用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"]); 

#4


2  

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(); }

#5


2  

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()方法的参数

#6


1  

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

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

#7


-1  

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)
    });