The function baz
sets a variable x
to be 1 and then creates returns an object. This object has two fields one being foo
and the other being bar
. A little quick thing about objects is that if you want to access a field you simply need to call object.fieldname
. We'll get more into that later.
The functions foo
and bar
are pretty simply. If you aren't sure what ++x and --x does here is a good Stackoverflow question:
Incrementing in C++ - When to use x++ or ++x?
Basically it is saying it will perform the statement and then increment or decrement x. Because it is returning x I believe it will return 1, and then increment or decrement x.
At the end of the function a variable called closures
is defined and this is the object that is returned from the function. Like I said before, in order to access the fields, all you would need to do is do object.fieldname
which in this case would be closures.foo()
or closures.bar()
(the parentheses are necessary to run the function).
I just tested it out and your alert shows 2. I believe the reason is that it doesn't understand the syntax of alert(int,int)
so it shows the first alert only.