Tips for debugging console. log in JavaScript,

Source: Internet
Author: User

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.logDebugging, the following article will share with you a JavaScriptconsole.logTips 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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.