A brief discussion on debugging javascript_basic knowledge
I have been complaining a lot recently. As we all know, the web front-end has become very heavy compared to a few years ago. There are various js frameworks, various objects, and when there are too many projects, public modules will be extracted.
The UI display of these modules is the same, the difference is the background logic. For example, when we do corporate travel, we usually have a js public module for the cost center, which customers fill in when booking air tickets. This cost center is distributed in online, offline and app booking terminals, which also facilitates monthly settlement with client companies in the future.
We also know that as the project gets bigger and more complicated, and becomes SOA, many problems arise. Just like a theory in the web, all front-end data is untrustworthy, so the interface of the other team Data is not the same. When the project was small in the past, I would not be so unconfident, and would only record logs when there were Logic errors. Normal business processes were rarely recorded. After all, the info logs did not look beautiful, and they would also Consuming server bandwidth will also drag down web performance.
But the project is getting bigger. When you encounter a strange bug in the project one day, you rely on the incomplete logs and finally trace the interface line by line with the naked eye. However, there are too many parameters and you cannot accurately restore the interface. Parameter data, but you are 100% confident that it must be the interface return problem, but you can't get the complete message. At this time, you can't find the interface provider. At that time, you were helpless. It's better to think about it every time. It would be nice to have logs for every line.
After learning the lesson, the trend of keeping process logs became more and more popular, and eventually it led to a big event at the beginning of the year. A lot of things were said in a confused way. If the web backend is like this, then the current front-end needs to be the same. Keep a log? We know that since it is a public js module, this module must have encapsulated some methods. It is definitely not allowed for third-party programs to operate its own text nodes, such as the following:
Company:
Employee name:
In order to simplify the operation, the third-party UI provides UI nodes for company names and employee names, and encapsulates a costcenter class to provide reading methods. As you can see, my reservation program only needs to read costCenter.getInfo. , and also plays a role of encapsulation.
But the problem arises here. In actual project implementation, the value cannot be obtained in costcenter due to various reasons. Of course, it may also be a bug in common ui.
But at that time you were not very sure whether the value was really obtained, but logically even if the value was not obtained, in principle you could not prevent the order from being submitted, so in order to thoroughly track the bug, I wrote a logCenter singleton class to record logs. This method is usually used to record logs using js.
<1> ajax
This method is easy to think of, but if you use the native xmlhttprequest, you also need to consider browser compatibility. But if you don’t use the native one, you have to rely on a third-party framework, such as jquery, but after all, there are still many companies that don’t use it. jquery, so this should be used according to actual needs.
//Log Center
var logCenter = (function () {
var result = {
info: function (title, message) {
//ajax operation
$.get("http://xxx.com", { "title": title, "message": message }, function () {
}, "post");
}
};
return result;
})();
<2>image
There is an object called image in our dom, so we can achieve the purpose of requesting the background URL by dynamically assigning a value to its src. At the same time, we need to pass the title and message information in the URL. This dynamically adds value to the image. The .src method does not need to consider browser compatibility issues, which is very good.
//Log Center
var logCenter = (function () {
var result = {
info: function (title, message) {
//ajax operation
$.get("http://xxx.com", { "title": title, "message": message }, function () {
}, "post");
},
info_image: function (title, message) {
//image
var img = new Image();
img.src = "http://www.baidu.com?title=" title "&message=" message "&temp=" (Math.random() * 100000);
}
};
return result;
})();
The above is the main content of this article, we will continue to discuss it in depth in the future

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

C++ multi-thread debugging can use GDB: 1. Enable debugging information compilation; 2. Set breakpoints; 3. Use infothreads to view threads; 4. Use thread to switch threads; 5. Use next, stepi, and locals to debug. Actual case debugging deadlock: 1. Use threadapplyallbt to print the stack; 2. Check the thread status; 3. Single-step the main thread; 4. Use condition variables to coordinate access to solve the deadlock.

How to use LeakSanitizer to debug C++ memory leaks? Install LeakSanitizer. Enable LeakSanitizer via compile flag. Run the application and analyze the LeakSanitizer report. Identify memory allocation types and allocation locations. Fix memory leaks and ensure all dynamically allocated memory is released.

This article introduces shortcuts for Go function debugging and analysis, including: built-in debugger dlv, which is used to pause execution, check variables, and set breakpoints. Logging, use the log package to record messages and view them during debugging. The performance analysis tool pprof generates call graphs and analyzes performance, and uses gotoolpprof to analyze data. Practical case: Analyze memory leaks through pprof and generate a call graph to display the functions that cause leaks.

Efficiently debug Lambda expressions: IntelliJ IDEA Debugger: Set breakpoints on variable declarations or methods, inspect internal variables and state, and see the actual implementation class. Java9+JVMTI: Connect to the runtime JVM to obtain identifiers, inspect bytecode, set breakpoints, and monitor variables and status during execution.

Concurrency testing and debugging Concurrency testing and debugging in Java concurrent programming are crucial and the following techniques are available: Concurrency testing: Unit testing: Isolate and test a single concurrent task. Integration testing: testing the interaction between multiple concurrent tasks. Load testing: Evaluate an application's performance and scalability under heavy load. Concurrency Debugging: Breakpoints: Pause thread execution and inspect variables or execute code. Logging: Record thread events and status. Stack trace: Identify the source of the exception. Visualization tools: Monitor thread activity and resource usage.

Tools for debugging PHP asynchronous code include: Psalm: a static analysis tool that can find potential errors. ParallelLint: A tool that inspects asynchronous code and provides recommendations. Xdebug: An extension for debugging PHP applications by enabling a session and stepping through the code. Other tips include using logging, assertions, running code locally, and writing unit tests.

Common PHP debugging errors include: Syntax errors: Check the code syntax to make sure there are no errors. Undefined variable: Before using a variable, make sure it is initialized and assigned a value. Missing semicolons: Add semicolons to all code blocks. Function is undefined: Check that the function name is spelled correctly and make sure the correct file or PHP extension is loaded.

C++ debugging functions that contain exception handling uses exception point breakpoints to identify exception locations. Use the catch command in gdb to print exception information and stack traces. Use the exception logger to capture and analyze exceptions, including messages, stack traces, and variable values.
