0

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?

phant0m
17k6 gold badges52 silver badges84 bronze badges
asked Jan 14, 2013 at 23:28
1
  • 1
    You should regard () as a postfix operator, that can be applied to anything that has a value. Commented Jan 14, 2013 at 23:35

4 Answers 4

2

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.

answered Jan 14, 2013 at 23:38
Sign up to request clarification or add additional context in comments.

5 Comments

How does this relate to JavaScript's weak type system?
@phant0m Because we can never actually know the type of the parameter. Unlike in strictly typed languages like C++ where we can restrict all but a function pointer (callback) to be passed.
You can make that argument for every function and its argument--no matter whether they are supposed to be functions or not--you define in JavaScript, why is this worth pointing out here? That is the part I don't understand.
Because normally i would expect arguments to be literals or variables, not an actual function. Thats what got me when i started learning this
You're making an artificial distinction here. You always pass objects, never variables. -- I like the new wording much better, +1.
1

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.

answered Jan 14, 2013 at 23:29

2 Comments

Thanks, is there any advantage of using that instead of this:var c = function (b) { function a (b){}; };
The advantage of your original example would be that you could pass any function object to c and it would run it. In your example here in the comments, the only function that can be run is the one defined inside the function definition for c. One is not better than the other, it depends on the use case.
1

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')
answered Jan 14, 2013 at 23:30

Comments

1

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

answered Jan 14, 2013 at 23:53

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.