Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When does console.log get executed?

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;
like image 386
iforce2d Avatar asked Nov 04 '12 11:11

iforce2d


People also ask

How does a console log work?

The console. log() is a function in JavaScript that is used to print any kind of variables defined before in it or to just print any message that needs to be displayed to the user.

Why does console log disappear immediately?

This is happening because your form is submitting to itself and the page loads in a fraction of seconds for you to notice the difference. There is 2 way you can solve this in your case. One: Add onsubmit="return false;" attribute in your form element.

What is the result of console log?

The console. log() method outputs a message to the web console. The message may be a single string (with optional substitution values), or it may be any one or more JavaScript objects.

Where does console log show up?

Steps to Open the Console Log in Google Chrome By default, the Inspect will open the "Elements" tab in the Developer Tools. Click on the "Console" tab which is to the right of "Elements". Now you can see the Console and any output that has been written to the Console log.


1 Answers

Immediately, 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

like image 147
Esailija Avatar answered Sep 21 '22 02:09

Esailija