开发者

Passing a function as an argument in a javascript function

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 开发者_JAVA百科integer argument for another function called 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].)


I think this is what you meant.

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

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

}


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!'


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


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


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


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


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

get method as a parameter:

 function JS_method(_callBack) { 

           _callBack("called");  

        }

You can give as a parameter method:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜