JavaScript調試代碼 (JavaScript debugging the code)
Debugging is the process of finding mistakes or bugs in the program. There are several ways one can debug their JavaScript code. This article will walk you through the strict mode in JavaScript and exception handling.
調試是發現程序中錯誤或錯誤的過程。 一種可以調試其JavaScript代碼的方法 。 本文將向您介紹JavaScript和異常處理中的嚴格模式 。
嚴格模式 (The Strict Mode)
JavaScript has an in built strict mode which when used, strictly checks the code for any kind of errors. Some of the flexibility that is usually allowed in the language is barred in the strict mode so that the user writes pure, clean error free code thereby preventing bugs from occuring in the first place. Let's look at some examples,
JavaScript具有內置的嚴格模式 ,該模式在使用時會嚴格檢查代碼是否存在任何類型的錯誤。 在嚴格模式下禁止使用該語言通常允許的某些靈活性,以便用戶編寫純凈,干凈的無錯誤代碼,從而從一開始就防止錯誤發生。 我們來看一些例子
//function definition
function print() {
for (i = 0; i < 3; i++) {
console.log(i);
}
}
//calling the function
print();
If you run this code, you'll get on the console,
如果您運行此代碼,則會進入控制臺,
0
1
2
Your code runs perfectly fine producing the correct output even though it had an error. Variable i was not declared anywhere and directly initialized and used. JavaScript reads your code and understands that you must have forgotten to declare i so it does that for you while executing the code. However, running the same code in strict mode,
即使有錯誤,您的代碼也可以正常運行,并產生正確的輸出。 變量i沒有在任何地方聲明,而是直接初始化和使用。 JavaScript會讀取您的代碼,并了解您一定忘記了聲明i,因此它在執行代碼時會為您這樣做。 但是,在嚴格模式下運行相同的代碼,
"use strict";
//function definition
function print() {
for (i = 0; i < 3; i++) {
console.log(i);
}
}
//calling the function
print();
Reports an error saying ReferenceError: i is not defined.
報告錯誤,指出ReferenceError:未定義。
function person(name){
this.name=name;
}
let fuzzy=person("Fuzzy");
Can you guess what's wrong with the above code?
您能猜出上面的代碼有什么問題嗎?
We have omitted the new keyword before person("Fuzzy") while declaring an object of person type. However, this code runs perfectly fine without producing any errors.
在聲明人員類型的對象時,我們在person(“ Fuzzy”)之前省略了新關鍵字 。 但是,此代碼運行得很好,不會產生任何錯誤。
But if we run this in strict mode,
但是如果我們在嚴格模式下運行 ,
"use strict";
function person(name){
this.name=name;
}
let fuzzy=person("Fuzzy");
We get an error saying TypeError:Cannot set property of 'name' undefined.
我們收到一條錯誤消息,提示TypeError:Cannot set property of'name'undefined。
strict mode disallows giving a function multiple parameters with the same name and removes certain problematic language features.
嚴格模式不允許為函數提供多個具有相同名稱的參數,并刪除某些有問題的語言功能。
Exception Handling
異常處理
It a mechanism through which code throws an exception when it runs into a program.
它是一種機制,代碼在運行到程序中時會通過該機制引發異常。
An exception can be any undesired value that we have got because of several reasons. It does not necessarily imply that our code had an abrupt logic or incorrect syntax, it simply means we got something that we didn’t want and it could also be because of interaction with some foreign API. Exception jumps down to the first call that started the current execution therefore unwinding the call stack and throws away all the call context it encounters.
異常可能是由于多種原因而獲得的任何不希望的值。 這不一定意味著我們的代碼具有突然的邏輯或不正確的語法,僅表示我們得到了我們不想要的東西,也可能是由于與某些外部API的交互。 Exception跳轉到開始當前執行的第一個調用,因此取消了調用堆棧,并丟棄了它遇到的所有調用上下文。
function promptDirection(question) {
let result = prompt(question);
if (result.toLowerCase() == 'left') return 'L';
if (result.toLowerCase() == 'right') return 'R';
throw new Error('Invalid directions: ' + result);
}
function Look() {
if (promptDirection('Which way?') == 'L') return 'a house';
else return 'Two angy beans';
}
try {
console.log('You see', look());
} catch (err) {
console.log('Something went wrong ' + err);
}
Output
輸出量
Something went wrong ReferenceError: look is not defined
The throw keyword raises an exception and catching is done by wrapping a piece of code in a try block followed by the catch keyword.
throw關鍵字引發異常,通過將一段代碼包裝在try塊中,然后加上catch關鍵字來完成捕獲 。
翻譯自: https://www.includehelp.com/code-snippets/debug-javascript-code.aspx