In the following example: http://jsfiddle.net/maniator/ScTAW/4/
I have this js:
var storage = (function () {
var store = [];
When you're calling console.log
like this
console.log(storage, storage.get(), storage.add('hi there #2'));
storage.add('hi there #2')
is evaluated and the return value is passed to console.log
. Evaluating it causes the array item to be added to store
immediately.
Same thing with storage.get()
-> store
. So effectively, the statement becomes:
console.log(storage, store, [return value of add which is undefined]);
When it prints, store
is evaluated and its content are output which is why you see ["hi there", "hi there #2"]
In your second example also, the anonymous function is evaluated first and the results are passed on.