How does console.log work?

后端 未结 4 603
清歌不尽
清歌不尽 2021-02-04 08:22

First Example:

In the following example: http://jsfiddle.net/maniator/ScTAW/4/
I have this js:

var storage = (function () {
    var store = [];
            


        
4条回答
  •  伪装坚强ぢ
    2021-02-04 08:54

    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.

提交回复
热议问题