console.log如何工作? [英] How does console.log work?

查看:242
本文介绍了console.log如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第一个示例:



在以下示例中: http:/ /jsfiddle.net/maniator/ScTAW/4/

我有这个js:

  var store =(function(){
var store = [];
return {
add:function(item){
store.push(item);
},
get:function(){
return store;
}
};
}

storage.add('hi there')
console.log(storage,storage.get(),storage.add('hi there#2'));

这里是打印到控制台的内容:


对象
[hi there,hi there#2] undefined


有人会认为控制台只能说:


物件
[hi there] undefined


因为不会发生,直到记录该值,因此不应该显示。 p>




第二个示例:



在以下示例中: http://jsfiddle.net/maniator/ScTAW/5/



我使用相同的存储变量,但我日志如下:

  storage.add('hi there')
console.log(storage,storage.get(),(function(){
storage.add('hi there#2');
console.log('TESTING');
})());

打印到控制台的是:



< blockquote>

测试

对象
[hi there,hi there#2] undefined


hmmmm这是奇数现在isnt吗?可以看到:


对象
[hi there]未定义

TESTING


为什么会发生这种情况?

解决方案

在大多数(如果不是全部)命令式编程语言中,任何在调用函数之前,必须评估传递给函数调用的参数(因此称为渴望评估)。此外,它们通常按从左到右的顺序(对于C,例如它未定义)进行评估,但是在这两个示例中,参数的计算顺序无关紧要。这应该是非常明显的,当看看发生了什么细节:



如上所述,在 console.log 可以调用, storage.get()必须先执行,返回 store 数组。那么 storage.add('hi there#2')将被执行(或者反过来),所以它的结果(在这种情况下 undefined ,因为添加不返回任何内容)可以作为第三个参数传递给 console.log 。这意味着将使用参数(storage,storage.store,undefined)调用once console.log store 数组已包含hi there#2,因此生成您观察到的结果。



示例的推理是一样的,函数调用只是有点更加模糊。首先看看它有一个函数作为第三个参数传递给 console.log 函数;但它实际上是一个函数调用(观察者()在结尾)。所以 storage.add('hi there#2')将被执行,然后 console.log('TESTING')然后来自匿名函数执行的 undefined 结果将再次传递到 console.log



如果你实际上传递一个函数到 console.log ,它将打印该函数定义,而不执行任何东西。所以:

  storage.add('hi there')
console.log(storage,storage.get ,(function(){
storage.add('hi there#2');
console.log('TESTING');
}))

,而不是() ,结果:

  Object 
[hi there] function(){
storage.add ('hi there#2');
console.log('TESTING');
}

我希望这可以使事情更清楚。


First Example:

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

var storage = (function () {
    var store = [];
    return {
        "add": function (item) {
            store.push(item);
        },
        "get": function () {
            return store;
        }
    };
}());

storage.add('hi there')
console.log(storage, storage.get(), storage.add('hi there #2'));

And here is what gets printed to the console:

Object ["hi there", "hi there #2"] undefined

One would think that the console should only say:

Object ["hi there"] undefined

becase the second push did not happen until after the value was logged, therefore it should not be displayed.


Second Example:

In the following example: http://jsfiddle.net/maniator/ScTAW/5/

I am using the same storage variable but I log like so:

storage.add('hi there')
console.log(storage, storage.get(), (function() {
    storage.add('hi there #2');
    console.log('TESTING');
})());

What gets printed to the console is:

TESTING
Object ["hi there", "hi there #2"] undefined

hmmmm well that is odd now isnt it? One could expect to see:

Object ["hi there"] undefined
TESTING

Why is this happening? What is going on behind the scenes of the console logging mechanism?

解决方案

In most (if not all) imperative programming languages, any arguments passed to a function call have to be evaluated before the function can be called (so called Eager evaluation). Also, they are in general evaluated in order from left to right (for C for instance it's undefined), however in both examples the order in which the arguments are evaluated does not matter. This should be pretty obvious when looking at what happens in detail:

As mentioned, before console.log can be called, storage.get() has to be executed first, returning the store array. Then storage.add('hi there #2') will be executed (or the other way round), so its result (in this case undefined, since add does not return anything) can be passed as the third argument to console.log. This means that the once console.log will be called with the arguments (storage, storage.store, undefined), the store array already contains "hi there #2", hence producing the results you observe.

In the second example the reasoning is again the same, the function call is just a bit more obscured. On first look it looks there is a function passed as a 3rd argument to the console.log function; but it's actually a function call (observer the () at the end). So storage.add('hi there #2') will be executed, then console.log('TESTING') and then the undefined result from the anonymous function execution will be again passed to console.log.

If you did actually pass a function to console.log, it would print that function definition, and not execute anything. So:

storage.add('hi there')
console.log(storage, storage.get(), (function() {
    storage.add('hi there #2');
    console.log('TESTING');
}));

, without the () at the end, results in:

Object
 ["hi there"] function () {
    storage.add('hi there #2');
    console.log('TESTING');
}

I hope this makes things a bit clearer.

这篇关于console.log如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆