swayziak
swayziak

Reputation: 737

Javascript parameter inside an other parameter

What does it mean when you have a parameter inside an other parameter in a function?

For example:

var c = function (a,b) {
    a(b);
};

What does a(b)do?

Upvotes: 0

Views: 89

Answers (4)

yoav barnea
yoav barnea

Reputation: 5994

JavaScript treats functions as a first class objects. This means that you can pass function as parameters. (it is a little like delegates in C#)

In your case, a is a parameter that points to a function and you execute it inside the function represented by c . (a itself, using b as it's first argument).

Upvotes: 1

David G
David G

Reputation: 96810

We know that an expression of the form f(x) is a call to the function f taking a single argument x. In addition, JS allows us to pass function callbacks as parameters to other functions. For instance:

function callback(x) { alert(x); }

c(callback, 5);

The callback takes a single argument, and that other argument 5 is passed as a parameter to the callback. In turn, it alerts the number.

Upvotes: 2

Joseph
Joseph

Reputation: 119837

This means that a should be a function which gets executed when it's passed to c, along with another argument.

An example of usage:

//c accepts a function and an argument
c(function(theSentB){
  //this passed function gets executed due to a(b);
  //and 'hello' is passed to it

  alert(theSentB);

},'hello')

Upvotes: 1

timc
timc

Reputation: 2174

In this case c is a function that can be passed two objects, a and b. From the definition it looks like a should be a function object and b a parameter for that function.

Within the definition you have a function called a being passed an argument of the object b.

Find out more about functions in javascript.

Upvotes: 1

Related Questions