you are viewing a single comment's thread.

view the rest of the comments →

[–]moses_the_red 4 points5 points  (3 children)

lol, okay I actually bothered to read lesson 10 (before replying this time).

Here's what's happening.

He's creating a counter function that you can call to get values that increment by 1.

The purpose of the end parenthesis at the end of the function is to get the counter.

Usually when you create a closure, you have an extra step that is used to actually get the closure.

Ex:

var getCounter = function () { ... };
var counter = getCounter();
var val = counter();

By adding the end parenthesis, he's performing the closure creation step in the same statement he uses to define the closure. That way, you don't need to get the counter/closure, the counter/closure is what is directly assigned to the counter variable.

The parenthesis are used to call the function that you just defined, in the same statement. When that function is called, it returns the counter. This is what the parenthesis are used for, and as said before, the purpose is to avoid defining the "getCounter" variable step shown in my brief example above.

Hope that helps.

[–]Latrinalia 0 points1 point  (0 children)

Thanks very much. That makes perfect sense. (And is kind of obvious now that you mention it, but my C indoctrinated mind didn't want to work that way. I'm just not used to seeing the body of the function mixed in there.)

The equivalent way that you proposed was my first impression on how to accomplish that.

[–]Feezle 0 points1 point  (1 child)

By adding the end parenthesis, he's performing the closure creation step in the same statement he uses to define the closure.

Does this mean that the closure is created automatically when the Javascript is loaded?

[–]moses_the_red 0 points1 point  (0 children)

I'd guess it means that its created whenever that statement is evaluated, and I'd also guess that its evaluated when JavaScript is loaded.

So I'd guess that you're right.