Example Analysis of console.log debugging for JavaScript debugging
This article will explain in detail the example analysis of console.log debugging for JavaScript debugging. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.
Preface
Using console.log () is a better way to debug JavaScript programs than alert (), because the alert () function will block the execution of JavaScript programs, resulting in side effects.
The alert pop-up box requires a click to confirm, while console.log () only prints the relevant information in the console, so it does not cause similar concerns.
The most important thing is that alert can only output strings, not the structure inside the object. Console.log () can accept any string, number and JavaScript object. You can see the object property structure clearly. It is very convenient to debug when ajax returns the json array object.
/ / compatible Firefox/IE/Opera uses console.logif (! window.console) {window.console = {log: function () {}};} window.console = window.console | | {}; console.log | | (console.log = opera.postError)
Here are two printed pictures of information:
Console.log debugging is briefly introduced above. Here is a tip on console.log debugging in JavaScript. Let's take a look at the detailed introduction.
Console gives the correct value
Let's take a look at this code directly.
Var obj = {name: 'little fool', age: 12} console.log (obj) obj.name = 'big fool'
Obviously I added console on the fourth line because I wanted to see the value of obj on the fourth line.
But the result is not satisfactory, it will be printed out.
{name: "Big fool", age: 12}
The reason is that obj is a referential variable, and the operations behind console will also affect the content of console.
Let's take a look at this code.
Var obj = {name: 'little fool', age: 12} console.log (obj.name) obj.name = 'big fool'
The printed result at this time is the expected little fool.
Solution
It is impossible for us to print all the properties of obj, because this is not realistic. We still want to print obj but get the result in the current location, I will post my own solution below
Console.log (JSON.parse (JSON.stringify (obj)
Deep copy through JSON is the simplest and most effective method I know.
This is the end of this article on "sample Analysis of console.log debugging for JavaScript debugging". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.