Debugging ability is the foundation of a programmer's survival , But many beginners ignore debugging . Today we're going to talk about JS Debugging skills .
This article will list in detail JS Related to a variety of practical debugging skills . If you are JS Beginners , So this article will be of great help to you .
Why debug ？
A program is a stack of functions , The operation of a program is the execution of a function . And by JS debugging , We can more intuitively trace the running of the program , The order in which functions are executed , And the change of each parameter . So we can quickly locate the problem .
1 What is? JS debugging ？
While the program is running , We always meet all kinds of bug, The process of tracking the running order of the code to locate the problem is called JS debugging .
First of all, we need to know how to enter the debugging interface , Here we take Google as an example , adopt F12 And right click to check , find Sources You can enter the debug interface . The specific interface is shown as follows ：
The red box indicates the buttons that we use to debug . Next, we will describe the function of each button in detail in the actual application scenario .
2 Step by step debugging
First, general debugging , It's also called single step debugging .F12 find Sources after , Find the file you want to run in the folder on the left , And then click pause script execution Button to refresh the page （F5）, You can go into single step debugging
Click on Step over next function call It's program debugging step by step , Every click , It's in the order of code execution , Execute one sentence of code down .
3 Function debugging
If you have tried single step debugging, you will find that , Single step debugging is not enough for us to find bug The needs of , Because single step debugging can't enter the function body , We can't keep track of variables in a function .
So we're going to learn about the third button ,step into next function call Button
Use Step into Button , We can go into the body of the function , In the process of single step debugging , When a function is called , Click on Step into You can enter the body of the function .
After entering the function body , Continue clicking Step over Button , You can do single step debugging in the function body . As shown in the figure , The change of variables in the function is clear at a glance .
But when we've tracked the changes we want , There's a lot in the function body , Single step debugging to the end of the function is a waste of time . Here you can use the fourth button we learned today ,step out of current function call Jump out of the body of the current function , Jump out to the code position that entered the function body before .
4 Breakpoint debugging
In actual projects, the amount of code is huge , Using single step debugging is a waste of time . And sometimes we want to find out if there is something wrong with the code , So there's no need to debug all the code . So we can use breakpoint debugging , So what is breakpoint debugging ？
First , Where you want to locate the problem , Breaking point , Where the code stops executing . As shown in the figure, click on the red box to break the line .（ Because of the different versions of Google , The breakpoint icon is slightly different ）
And then refresh the page （F5）, You can enter the breakpoint debugging page . Click on Resume script execution Button can be adjusted to the next breakpoint .（ Be careful ： When it reaches the breakpoint , This line of code has a blue background , Indicates that this line of code is about to be executed but not executed ）
5 Event debugging
First of all, let's define a concept , It's synchronous and asynchronous , When the code is executed , The top-down run is synchronization code , One of the types of asynchronous code is that it needs events to trigger . So in code debugging , Functions in the event body , It needs to be after the break point , Through the behavior of the event , To enter the function body .
As the following code ： Refresh after break point , It doesn't work
Debugging must be triggered by an event