Netease front-end micro-Professional, JavaScript programming: JS debugging
This section describes how to debug JS by using an example. First run the Code:
Test.html:
Calculator + = Computing<Script src = "./demo. js"> </script>
Demo. js
/** * Created by yanzi on 15/12/8. */var num1 = document.getElementById("num1"), num2 = document.getElementById("num2"), result = document.getElementById("result"), btn_add = document.getElementById("add");btn_add.addEventListener("click", onAddClick, false);function onAddClick(){ var a = parseInt(num1.value), b = parseInt(num2.value); var sum = add(a, b); result.innerHTML = sum;}/** * * @param a * @param b * @returns {*} */function add(a, b){ return a+b;}
Effect:
Classroom summary:
1. In general, there are three types of JS debugging and printing information:
A. Use alert. The disadvantage is that the dialog box is displayed every time.
B. You can use console. log to handle a small amount of data.
C. breakpoint debugging
2. In JS, if var is added before the variable, it indicates the local variable. If var is not included in the function, it indicates the global variable. Therefore, the variable is usually preceded by var.
3. In the chrome debugger, elements looks at the code and debug it in the source directory. In this mode, you can click each line in js to add a breakpoint.
4. In debug mode, the four buttons on the rightmost side are: Next breakpoint, one-step execution, go to the next function, and jump out of the function. Basically all debugging tools have these four types.
5. In breakpoint mode, enter a variable in the console to view the current value, and modify the value at will.