日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

调试JavaScript代码

發布時間:2023/12/1 javascript 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 调试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

總結

以上是生活随笔為你收集整理的调试JavaScript代码的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。