Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing arguments in anonymous functions in JavaScript

Tags:

Sometimes I see JavaScript that is written with an argument provided that already has a set value or is an object with methods. Take this jQuery example for instance:

$(".selector").children().each(function(i) {     console.log(i); }); 

When logging i, you would get the value of whatever i is in that iteration when looking at the selectors children in the jQuery each method.

Take this Node.js example:

http.createServer(function(request, response) {     response.writeHead(200, {"Content-Type": "text/plain"});     response.write("Hello World");     response.end(); }).listen(8888); 

You can see here that request and response are being passed and they contain their own methods that can be acted on.

To me, this looks like were passing a function to the createServer function with two arguments that have methods already attached to them.

My question is a multipart one:

  1. Where do these arguments come from?
  2. If these are just anon functions, how do they receive arguments that can be acted on like other functions?
  3. How do I create functions that can take my own arguments like this??
  4. Does this use the power of closures??
like image 595
Sethen Avatar asked Aug 12 '13 20:08

Sethen


People also ask

Can anonymous functions take arguments?

An anonymous function is not accessible after its initial creation, it can only be accessed by a variable it is stored in as a function as a value. An anonymous function can also have multiple arguments, but only one expression.

How passing arguments work in JavaScript?

Arguments are Passed by Value The parameters, in a function call, are the function's arguments. JavaScript arguments are passed by value: The function only gets to know the values, not the argument's locations. If a function changes an argument's value, it does not change the parameter's original value.

Can we assign an anonymous function to a variable in JavaScript?

An anonymous function in javascript is not accessible after its initial creation. Therefore, we need to assign it to a variable, so that we can use its value later. They are always invoked (called) using the variable name. Also, we create anonymous functions in JavaScript, where we want to use functions as values.

Can you assign an anonymous function to a variable and pass it as an argument to another function example?

They're called anonymous functions because they aren't given a name in the same way as normal functions. Because functions are first-class objects, we can pass a function as an argument in another function and later execute that passed-in function or even return it to be executed later.


1 Answers

To me, this looks like were passing a function to the createServer function with two arguments that have methods already attached to them.

No. They were passing a function to createServer that takes two arguments. Those functions will later be called with whatever argument the caller puts in. e.g.:

function caller(otherFunction) {      otherFunction(2);  } caller(function(x) {     console.log(x);  }); 

Will print 2.

More advanced, if this isn't what you want you can use the bind method belong to all functions, which will create a new function with specified arguments already bound. e.g.:

caller(function(x) {     console.log(x); }.bind(null, 3); }); 

Will now print 3, and the argument 2 passed to the anonymous function will become an unused and unnamed argument.

Anyway, that is a dense example; please check the linked documentation for bind to understand how binding works better.

like image 57
djechlin Avatar answered Sep 29 '22 18:09

djechlin