調試JavaScript代碼

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

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/377339.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/377339.shtml
英文地址,請注明出處:http://en.pswp.cn/news/377339.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Delphi運算符及優先級

單目運算符 (最高優先級) 取變量或函數的地址(返回一個指針) not 邏輯取反或按位取反 乘除及按位運算符 * 相乘或集合交集 / 浮點相除 div 整數相除 mod 取模 (整數相除的余數) as 程序運行階段類型轉換 (RTTI運算符) and 邏輯或按位求和 shl 按位左移 shr 按位右移 加減運算符…

NotifyMyFrontEnd 函數背后的數據緩沖區(二)

message level 函數pq_putmessage調用 low level 函數 pq_putbytes,pq_putbytes調用 internal_putbytes。 從internal_putbyes上來看&#xff0c;就可以發現其數據發送的機制:有一個小技巧&#xff0c;如果數據緩沖區滿了&#xff0c;就發送&#xff0c;否則就先堆在那兒。如果…

從源碼角度剖析VC6下的內存分配與切割的運作

目錄前言1、heap初始化2、第一次分配內存&#xff0c;計算真正區塊大小3、new_region管理中心4、__sbh_alloc_new_group()切割第一次分配好的內存5、開始切割內存前言 malloc與free帶來的內存管理是應付小區塊的&#xff0c;即SBH(small block heap)&#xff0c;這點也可以從源…

windows常見命令整理(持續更新)

windows常見命令整理 1. 文件1.1. 實時顯示文件 logfile.txt 中新添加的內容&#xff08;類似于linux tail -f&#xff09; 2. 網絡2.1. netstat 3. 進程和任務3.1. tasklist &#xff08;用于列出當前運行的進程及其詳細信息&#xff09;3.2. wmic &#xff08;用于執行各種系…

最長公共子序列求序列模板提_最長公共子序列

最長公共子序列求序列模板提Description: 描述&#xff1a; This question has been featured in interview rounds of Amazon, MakeMyTrip, VMWare etc. 這個問題在亞馬遜&#xff0c;MakeMyTrip&#xff0c;VMWare等訪談輪次中都有介紹。 Problem statement: 問題陳述&…

洛必達法則使用條件

使用條件 1、分子分母同趨向于0或無窮大 。 2、分子分母在限定的區域內是否分別可導。 3、當兩個條件都滿足時&#xff0c;再求導并判斷求導之后的極限是否存在&#xff1a;若存在&#xff0c;直接得到答案&#xff1b;若不存在&#xff0c;則說明此種未定式無法用洛必達法則解…

求根號m(巴比倫算法)

巴比倫算法是針對求根號m的近似值情況的&#xff0c;它的思想是這樣的&#xff1a; 設根號mX0,則如果枚舉有答案X(X<X0)&#xff0c;則m/X>X0,當精度要求不高的時候&#xff0c;我們可以看成Xm/XX0,而如果精度要求比較高&#xff0c;我們只需取X和m/X的平均值作為新的枚舉…

Android面試題

http://blog.csdn.net/aomandeshangxiao/article/category/841452 http://www.cppblog.com/life02/category/18316.html轉載于:https://www.cnblogs.com/DonkeyTomy/articles/2598673.html

r語言 分類變量 虛擬變量_R語言中的變量

r語言 分類變量 虛擬變量R語言| 變數 (R Language | Variables) In the previous tutorial, we have come across the basic information that stands as a pavement for understanding the R language in depth. Now moving future let us educate ourselves about the concep…

算法題復習(快排、鏈表、二分、哈希、雙指針)

目錄1、快速排序復習2、鏈表部分復習203. 移除鏈表元素707. 設計鏈表206. 反轉鏈表142.環形鏈表 II3、二分法復習4、哈希法復習5、雙指針復習**15. 三數之和****18. 四數之和****27. 移除元素****344. 反轉字符串**,簡單&#xff0c;雙指針從兩側往中間靠攏&#xff0c;并隨時s…

Cassandra1.2文檔學習(7)—— 規劃集群部署

數據參考&#xff1a;http://www.datastax.com/documentation/cassandra/1.2/webhelp/index.html#cassandra/architecture/architecturePlanningAbout_c.html 當規劃一個Cassandra集群部署時&#xff0c;關于你初始存儲的數據的數據量你應當有一個好的想法&#xff0c;并且對于…

虛擬機設置NAT

需要開啟虛擬機網絡相關服務&#xff0c; 安裝虛擬網卡&#xff0c; 還有必須安裝 VMware ToolsVMware虛擬機下實現NAT方式上網1. 把你的虛擬網卡VMnet8設置為自動獲得IP、自動獲得DNS服務器&#xff0c;啟用。2. 把你虛擬機中操作系統的“本地連接”也設置為自動獲得IP、自動獲…

窗體震動 C# (不使用Timer控件,控制窗體震動)

private static Point plocation new Point(); public static void StartVibration(Form form)//Form 傳入需要振動的窗體 { plocation form.Location; for (int i 1; i < 41; i)//41&#xff0c;可以理解為震動的時間。…

算法題復習(棧與隊列、二叉樹)

目錄棧與隊列棧用于匹配的問題隊列用于堆二叉樹系列深度遍歷&#xff0c;遞歸與迭代層序遍歷二叉樹屬性二叉樹修改與構造二叉搜索樹公共祖先二叉搜索樹的修改與構造棧與隊列 棧用于匹配的問題 20. 有效的括號 https://leetcode-cn.com/problems/valid-parentheses/ 不匹配的三…

bpsk_BPSK的完整形式是什么?

bpskBPSK&#xff1a;二進制相移鍵控 (BPSK: Binary Phase Shift Keying) BPSK is an abbreviation of "Binary Phase Shift Keying". BPSK是“二進制相移鍵控”的縮寫 。 BPSK is also occasionally called phase reversal keying (PRK), or 2PSK, which is the el…

win7 下安裝oracle 10g

oracle 10g 在win7下安裝&#xff0c;提示程序異常終止&#xff0c;發生未知錯誤 在網上搜結果&#xff1a; 修改Oracle 10G\database\stage\prereq\db\refhost.xml 在 </SYSTEM> <CERTIFIED_SYSTEMS>后面添加 <!--Microsoft Windows 7--> <OPERAT…

poj 1703 Find them, Catch them

題目鏈接&#xff1a;http://poj.org/problem?id1703 題目大意&#xff1a;警察抓獲N個罪犯&#xff0c;這些罪犯只可能屬于兩個團伙中的一個&#xff0c;現在給出M個條件&#xff08;D a b表示a和b不在同一團伙&#xff09;&#xff0c;對于每一個詢問(A a b)確定a&#xff0…

雙向a*搜索算法_雙向搜索算法

雙向a*搜索算法什么是雙音搜索&#xff1f; (What is bitonic search?) Searching a bitonic array is known as bitonic search. An array is said to be bitonic if it has an increasing sequence of integers followed immediately by a decreasing sequence of integers.…

關于LRU緩存簡單記錄以及代碼補全。

目錄大概思路時間空間復雜度分析指針操作具體細節代碼雙向鏈表設計私有成員變量設計:構造函數和析構函數設計&#xff1a;get與put具體設計雙向指針的具體細節添加到頭節點函數刪除尾節點函數刪除節點函數刪除節點函數感想今天面試考到LRU&#xff0c;太緊張了&#xff0c;完全…

碼農干貨系列【4】--圖像識別之矩形區域搜索

簡介 定位某個圖片的矩形區域是非常有用的&#xff0c;這個可以通過手動的選擇某個區域來實現定位&#xff0c;圖片相關的軟件都提供了這個功能&#xff1b;也可以像本篇一個通過程序來實現智能定位。前者會有誤差&#xff0c;效率低下&#xff1b;后者選區精度高&#xff0c;效…