Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of console.log debugging for JavaScript debugging

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report