36

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?

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

0

7 Answers 7

48

I think this is what you meant.

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

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

}
Sign up to request clarification or add additional context in comments.

6 Comments

+1 This is the correct answer, but could use a litle elaboration. In JavaScript, you can pass a function as an argument to another function (or assign it to a variable, etc.) but just referring to it by its name with no parentheses, like var func = foo or func(bar) (where foo and bar are functions). If you instead invoke the function by name with parentheses, like var value = foo() or func(bar()), then you are executing the function and the result of the function will be assigned to the variable or passed to the function, respectively.
I cleaned up the answer and added comments so it should be much more apparent what-is-what. If you run console.log(a+', '+typeof foo)); it will log z, function.
Furthermore to execute that function foo call it as you would any other function but as the parameter name it is assigned: foo(); in example so no need for eval(). :-)
@Sergei Golos - Hi I just wanted to know that how can we test these types of function, i.e., funct("z", function (x) { return x; });
Ideally, if you are working with functional programming your are also keeping your functions pure. Meaning that no mutation occurs and the same input will always generate the same output. This is a strength for unit testing, you can always measure the result and be sure that nothing outside the scope of the function have been effected. In the case of a function being the input into another function, the unit test should be interested in what the function you are testing does with the function you pass in. I am assuming that this is what you are specifically asking about. Right?
|
29

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

Comments

9

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

Comments

2

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

2 Comments

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]
@Spence Then I think you've got a good suggestion for such a use case in the answer by Sergei. He declares funct, which takes a function as a second argument and then uses it to operate on the first argument. The first line shows an example of a call with an anonymous function passed to funct as its second argument.
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:

function removeETC(delAction){ delAction(); }

Comments

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.