admin管理员组文章数量:1400211
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 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;
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 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;
Share
Improve this question
asked Nov 4, 2012 at 11:52
iforce2diforce2d
8,2723 gold badges33 silver badges42 bronze badges
1 Answer
Reset to default 9Immediately, but the state of objects is fetched when you expand the object manually in console - not at the time of the logging.
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
本文标签: javascriptWhen does consolelog get executedStack Overflow
版权声明:本文标题:javascript - When does console.log get executed? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744219830a2595812.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论