When is console.log executed?

I am trying to debug fairly simple Javascript using console.log, but it outputs values ​​for variables that don't change until after calling console.log when the variables are members of the class (Chrome 22, Firefox 16).

An example of what I expect will be as follows:

var a = 1; console.log(a); a += 20; //console output says a is 1 

But if the variable is a member of the class:

 var a = new myClass(1); console.log(a); ax += 20; //console output says ax is 21 

If the console does not register the value, how does it exist, when the log is called, when does it finally decide to write the value, and how can I get around this ??

fwiw is the complete 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); ax += 20; 
+4
source share
1 answer

Immediately, but the state of the objects is retrieved when the object is expanded manually on the console - not during logging.

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

+5
source

Source: https://habr.com/ru/post/1443934/


All Articles