Tips for debugging console. log in JavaScript,
Preface
For JavaScript program debugging, compared with alert (),console.log()
Is a better way, because:alert()
Functions block the execution of JavaScript programs and cause side effects;
In the alert pop-up box, you need to click "OK ".console.log()
Printing related information only on the console does not cause similar concerns.
The most important thing is that alert can only output strings and cannot output the structures in objects,console.log()
It can accept any string, number, and JavaScript Object. You can see the object property structure clearly, which is convenient for debugging when ajax returns a json array object.
// Compatible with Firefox/IE/Opera using console. logif (! Window. console) {window. console ={ log: function () {}};} window. console = window. console | |{}; console. log | (console. log = opera. postError );
The following two printed information images are shared:
The above briefly introducesconsole.log
Debugging, the following article will share with you a JavaScriptconsole.log
Tips for debugging:
The correct value is displayed on the console.
Let's take a look at this piece of code.
Var obj = {name: 'Little dump', age: 12} console. log (obj) obj. name = 'Big dump'
Obviously, I added the console in the fourth row to view the value of obj in the fourth row.
But the results are not satisfactory and will be printed out.
{Name: "Big Fool", age: 12}
The reason is that obj is a reference variable, and operations after the console will also affect the content of the console.
Let's take a look at this piece of code.
Var obj = {name: 'Little dump', age: 12} console. log (obj. name) obj. name = 'Big dump'
At this time, the printed result is the expected dumb.
Solution
We cannot print all the properties of obj, because this is unrealistic. We still want to print obj but get the result at the current position. I will post my Solution Below
console.log(JSON.parse(JSON.stringify(obj)))
Using the JSON Method for deep copy is the simplest and most effective method I know.
Summary
The above is all the content of this article. I hope the content of this article will help you in your study or work. If you have any questions, please leave a message, thank you for your support.