console.log什么时候执行? [英] When does console.log get executed?

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

问题描述

我正在尝试使用console.log调试一些非常简单的Javascript,但它输出的变量值在之后在console.log调用之后才会更改,当变量为'class'时成员(Chrome 22,Firefox 16)。

I'm trying to debug some pretty simple Javascript using console.log, but it's outputting values for variables that are not changed until after the console.log call, when the variables are 'class' members (Chrome 22, Firefox 16).

我期望发生的一个例子如下:

An example of what I expect to happen would be like this:

var a = 1;
console.log(a);
a += 20;

//console output says a is 1

但是如果变量是''类'成员:

But if the variable is a 'class' member:

var a = new myClass(1);
console.log(a);
a.x += 20;

//console output says a.x is 21

如果控制台没有记录调用日志时存在的值,它何时最终决定记录该值,以及如何解决这个问题??

If the console does not log the value as it exists when the log is called, when does it finally decide to log the value, and how can I work around this!?

这里是fwiw全部代码:

fwiw here is the entirety of the code:

function myClass() {
    myClass.myClass.apply(this, arguments);
    if (this.constructor === myClass) this.myClass.apply(this, arguments);
};
myClass.myClass = function () {};
myClass.prototype.myClass = function (x_) {
    if (x_ === undefined) x_ = 0;
    this.x = x_;
}

var a = new myClass(1);
console.log(a);
a.x += 20;


推荐答案

立即,但状态的对象<在控制台中手动展开对象时获取/ em>,而不是在记录时。

Immediately, but the state of objects is fetched when you expand the object manually in console - not at the time of the logging.

当你在控制台中展开它时,代码为添加20已经执行很久以前(相对而言),它说 x:21

By the time you expand it in your console, the code to add 20 has executed a long time ago (in relative terms) and it says x: 21

这篇关于console.log什么时候执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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