在javascript函数中将函数作为参数传递 [英] Passing a function as an argument in a javascript function
本文介绍了在javascript函数中将函数作为参数传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道这是否合法。我可以这样:
I was wondering whether this is legal to do. Could I have something like:
function funct(a, foo(x)) {
...
}
其中 a
是一个数组和 x
是另一个名为 foo
的函数的整数参数?
where a
is an array and x
is an integer argument for another function called foo
?
(我的想法是让一个函数在数组中使用for循环,并在数组中的每个元素的params中调用该函数。这个想法就是在不同的函数上调用它,这样两个数组的元素就会成倍增加然后将总和加在一起。例如 A [0] * B [0] + A [1] * B [1]
。)
(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]
.)
推荐答案
我认为这就是你的意思。
I think this is what you meant.
funct("z", function (x) { return x; });
function funct(a, foo){
foo(a) // this will return a
}
这篇关于在javascript函数中将函数作为参数传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文