0

So the have seem something like this today:

$$('input').each(function() {
 // do something
});

What is the difference between that and this:

$$('input').each( // do something );

The private function on the first example is within that for loop scope, so it is not reference-able anywhere else, so what would the benefit of having it?

Churk
  • 4,556
  • 5
  • 22
  • 37
  • 2
    ... not using a function literal would throw a syntax error, because arbitrary JavaScript can't be embedded in parentheses? – Ry- Mar 23 '12 at 14:51
  • 1
    You _could_ define an external function and pass it in to each like so: `function foo() { alert('foo!') }; $('.input').each(foo);` OR you could make an anonymous function and pass it in like you did in your first example: `$('.input').each( function() { alert('foo!') } );` – rcplusplus Mar 23 '12 at 14:54
  • @minitech, so what you are saying is I can have something like this? $(something).each( a=b; c=d; e=f); – Churk Mar 23 '12 at 15:01
  • @Churk: No, you can't have that. That's what closures (anonymous functions) are for. – Ry- Mar 23 '12 at 15:05
  • @minitech Ok, that make sense now. The point of having that anonymous functions and the added benefit of scoping. – Churk Mar 23 '12 at 15:09

3 Answers3

2

This is not private function, it's just an anonymous function (function without name).

You could do:

$('input').each(function() {
 // do something
});

Or you could do:

function foo() {
 // do something
}
$('input').each(foo);

Anyway, the .each method needs a function to be the parameter which is a callback.

xdazz
  • 158,678
  • 38
  • 247
  • 274
  • the .each is just something I picked out as an example, but you are right, the .each() is a callback, but I have seem this on non-callback iterating function. – Churk Mar 23 '12 at 15:00
2

The anonymous function knows its current scope and variables.

function a() {
   var foo = 'test';
   $('something').each(function() {
      alert(foo);
   })
}

This function would alert 'test' because foo is defined within its scope.

function a() {
   var foo = 'test';
   $('something').each(b)
}

function b() {
   alert(foo);
}

This function would cause a javascript error stating that the variable foo is undefined.

If you need to define a function that only makes sense within an exact scope and don't want to throw lots of variables around or recalculate a lot of things, it's more comfortable to define an anonymous function.

Mike
  • 2,567
  • 3
  • 23
  • 35
1

The first function is not private. It can reference all variables in the containing scope to form a closure, because JavaScript has lexical scoping.

The second function is pretty much the same thing, except the only thing you can do is provide it with a function name, or you will get a syntax error.

Short Answer: You put the function statement there to do more than one thing, or do one thing and provide parameters.

cmc
  • 4,294
  • 2
  • 35
  • 34
  • So the function allows you to pass in more parameter, such as $(a).each(function(c){ // do something with c}); where as I couldn't the other way. Ahhhh, thanks – Churk Mar 23 '12 at 14:58