javascript初學者_針對JavaScript初學者的調試技巧和竅門

javascript初學者

by Priyanka Garg

由Priyanka Garg

My intended audience for this tutorial is beginner programmers. You’ll learn about frustration-free debugging with chrome dev tools.

本教程的目標讀者是初學者。 您將學習使用chrome開發工具進行無挫折的調試。

Dear Beginner, a while ago, I was where you are! After a lot of struggles, I feel like I have come far in my learning journey. I am currently in the senior phase of an immersive bootcamp where I am building full stack applications.

親愛的初學者,前一陣子,我在你身邊! 經過很多努力,我覺得自己在學習過程中走了很遠。 我目前處于沉浸式訓練營的高級階段,在那里我將構建完整的堆棧應用程序。

Every day, I learn and stumble upon so many things, which I wished I knew before. This article is an attempt to share one such idea that will make your life easier.

每天,我學習并偶然發現了很多我以前希望知道的東西。 本文試圖分享一種這樣的想法,使您的生活更輕松。

As you must have learned, the best way to learn programming is by doing. Now, as you start practicing coding, sometimes (or most of the times) your code will not work; in other words, there will be a BUG in your code. And you may have already tried and learned some approaches to debugging. This article is not about any one debugging approach, but rather a setup to debug your code for programming practice.

正如您必須了解的那樣,學習編程的最佳方法是邊做邊學。 現在,當您開始練習編碼時,有時(或大部分時間)您的代碼將無法工作; 換句話說,您的代碼中將存在一個BUG。 您可能已經嘗試并學習了一些調試方法。 本文不涉及任何一種調試方法,而是一種用于調試代碼以進行編程實踐的設置。

If you are practicing on an online development environment, most probably you have a setup where you have an editor, a problem and a test suite that tests your program.

如果您在在線開發環境中進行練習,則很可能您會在安裝程序中擁有一個編輯器,一個問題和一個測試程序的測試套件。

You have written code, and there are some bugs, and at some point, the errors thrown up by the test suite are not really helpful.

您已經編寫了代碼,并且存在一些錯誤,并且在某些時候,測試套件引發的錯誤并沒有真正的幫助。

I won’t elaborate on how tedious debugging can become here — rather let me jump straight to some tips for beginners.

我不會詳細說明調試會變得多么繁瑣,而是讓我直接跳入門的一些技巧。

問題 (The problem)

As an example, I am writing a palindrome checker in FreeCodeCamp’s editor. My solution fails. In this case, we could use the test suite results to debug.

例如,我正在FreeCodeCamp的編輯器中編寫回文檢查器。 我的解決方案失敗。 在這種情況下,我們可以使用測試套件的結果進行調試。

But let’s assume this test suite doesn’t give me great pointers to the exact error. (This may not be the ideal example in terms of a logical error. The point is you will come across problems where the test suite will not directly point to a logical error.)

但是,讓我們假設這個測試套件沒有給我很好的指示確切錯誤的指針。 (就邏輯錯誤而言,這可能不是理想的例子。問題是您會遇到測試套件不會直接指向邏輯錯誤的問題。)

提示: 使用開發人員工具的控制臺。 (Tip: Use the console of developer tools.)

I run the same code in the console with the failing test case, and I see it returns ‘undefined’. That means the code did not return any value. I can quickly see that I forgot to return ‘true ’ if the string was found to be a palindrome.

我在測試用例失敗的情況下在控制臺中運行相同的代碼,并且看到它返回“未定義”。 這意味著代碼沒有返回任何值。 我可以很快看到,如果發現該字符串是回文,我忘記返回'true'。

This was a very simple error. Most of the times you would have bugs that need you to examine your variables. One approach to check variables is to console.log(<variables>) in the program.

這是一個非常簡單的錯誤。 在大多數情況下,您會遇到需要檢查變量的錯誤。 一種檢查變量的方法是在程序中使用console.log(<variable s>)。

However, I would suggest you use the dev tools debugger instead. In your program, you can specify the point where you want to start getting help from the debugger.

但是,我建議您改用開發工具調試器 。 在程序中,您可以指定要從調試器開始獲得幫助的位置。

The debugger will show you all the variables in the call stack and let you step through function calls, which you will find very useful.

調試器將向您顯示調用堆棧中的所有變量,并逐步執行函數調用,您會發現它們非常有用。

You will get the hang of using the debugger once you have used it a few times. Notice the arrows in the lower left box? These will let you control the program flow and show you the variables as they change.

一旦使用了幾次調試器,您將無所適從。 注意左下方的箭頭了嗎? 這些將使您可以控制程序流程并向您顯示變量的變化。

Now let’s head for a trick.

現在讓我們來個技巧。

技巧:進行個人調試設置 (Trick: Make a Personal Debugging Setup)

As seen above, with debugger and console, we can identify the problems easily. However, if I want to run the corrected program again on the console with just one line of change, I would have to re-type it.

如上所示,使用調試器和控制臺,我們可以輕松識別問題。 但是,如果只想更改一行就可以在控制臺上再次運行更正的程序,則必須重新輸入它。

Even after that, I get an error:

即使在那之后,我仍然得到一個錯誤:

This error is because I have used an arrow function, and cannot re-declare a const. This means that I would have to open a new tab and new console every time I change my code. Extra overhead, right?

此錯誤是因為我使用了箭頭功能,并且無法重新聲明const。 這意味著我每次更改代碼時都必須打開一個新的選項卡和新的控制臺。 額外的開銷,對不對?

Let's find a workaround. On your system, create a directory and cd into that directory.

讓我們找到一種解決方法。 在您的系統上,創建目錄并cd進入該目錄。

Now create two files: index.js and index.html. Type the following HTML in index.html:

現在創建兩個文件:index.js和index.html。 在index.html中輸入以下HTML:

Now move your code from the console to index.js. Notice I have started the debugger on line 2 in the code.

現在,將代碼從控制臺移至index.js。 注意,我已經在代碼的第2行啟動了調試器。

Now run the index.html file in the browser. Open the developer tools or console (you may have to refresh to see the debugger). You can debug your code here.

現在,在瀏覽器中運行index.html文件。 打開開發人員工具或控制臺(您可能必須刷新才能看到調試器)。 您可以在此處調試代碼。

Now every time you make a change to index.js you just hit refresh on this tab and the code reruns. No need to close and open tabs, no re-typing whole programs.

現在,每次對index.js進行更改時,只需在此選項卡上單擊“刷新”,然后代碼就會重新運行。 無需關閉和打開選項卡,無需重新鍵入整個程序。

Keep the folder you just created handy. Whenever you need to try or debug a piece of code, pop that into index.js and experiment!!

將剛剛創建的文件夾放在手邊。 每當您需要嘗試或調試一段代碼時,請將其彈出到index.js中并進行實驗!

總結思想 (Closing thoughts)

If you already knew this, congratulations you wasted a valuable 4 minutes ;)

如果您已經知道這一點,那么恭喜您浪費了寶貴的4分鐘;)

Finally, remember to err is human! Don’t worry about bugs — they will teach you the most valuable lessons of programming… and then ... Oh! the places you’ll go…

最后,記得犯錯是人類! 不用擔心bug,它們會教給您最有價值的編程知識……然后…… 哦! 你要去的地方

翻譯自: https://www.freecodecamp.org/news/debugging-javascript-for-beginners-5d4ac15dd1cd/

javascript初學者

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

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

相關文章

leetcode 705. 設計哈希集合

不使用任何內建的哈希表庫設計一個哈希集合&#xff08;HashSet&#xff09;。 實現 MyHashSet 類&#xff1a; void add(key) 向哈希集合中插入值 key 。 bool contains(key) 返回哈希集合中是否存在這個值 key 。 void remove(key) 將給定值 key 從哈希集合中刪除。如果哈希…

ecshop 前臺個人中心修改側邊欄 和 側邊欄顯示不全 或 導航現實不全

怎么給個人中心側邊欄加項或者減項 在模板文件default/user_menu.lbi 文件里添加或者修改,一般看到頁面都會知道怎么加,怎么刪,這里就不啰嗦了 添加一個欄目以后,這個地址跳的頁面怎么寫 這是最基本的一個包括左側個人信息,頭部導航欄 <!DOCTYPE html PUBLIC "-//W3C//…

leetcode 706. 設計哈希映射

不使用任何內建的哈希表庫設計一個哈希映射&#xff08;HashMap&#xff09;。 實現 MyHashMap 類&#xff1a; MyHashMap() 用空映射初始化對象 void put(int key, int value) 向 HashMap 插入一個鍵值對 (key, value) 。如果 key 已經存在于映射中&#xff0c;則更新其對應…

數據庫語言 數據查詢_使用這種簡單的查詢語言開始查詢數據

數據庫語言 數據查詢Working with data is becoming an increasingly important skill in the modern workplace. 在現代工作場所中&#xff0c;處理數據已成為越來越重要的技能。 Data is no longer the domain of analysts and software engineers. With todays technology,…

面向對象編程思想-觀察者模式

一、引言 相信猿友都大大小小經歷過一些面試&#xff0c;其中有道經典題目&#xff0c;場景是貓咪叫了一聲&#xff0c;老鼠跑了&#xff0c;主人被驚醒&#xff08;設計有擴展性的可加分&#xff09;。對于初學者來說&#xff0c;可能一臉懵逼&#xff0c;這啥跟啥啊是&#x…

typescript 使用_如何使用TypeScript輕松修改Minecraft

typescript 使用by Josh Wulf通過喬什沃爾夫(Josh Wulf) 如何使用TypeScript輕松修改Minecraft (How to modify Minecraft the easy way with TypeScript) Usually, modifying Minecraft requires coding in Java, and a lot of scaffolding. Now you can write and share Min…

Python:在Pandas數據框中查找缺失值

How to find Missing values in a data frame using Python/Pandas如何使用Python / Pandas查找數據框中的缺失值 介紹&#xff1a; (Introduction:) When you start working on any data science project the data you are provided is never clean. One of the most common …

監督學習-回歸分析

一、數學建模概述 監督學習&#xff1a;通過已有的訓練樣本進行訓練得到一個最優模型&#xff0c;再利用這個模型將所有的輸入映射為相應的輸出。監督學習根據輸出數據又分為回歸問題&#xff08;regression&#xff09;和分類問題&#xff08;classfication&#xff09;&#…

leetcode 54. 螺旋矩陣(遞歸)

給你一個 m 行 n 列的矩陣 matrix &#xff0c;請按照 順時針螺旋順序 &#xff0c;返回矩陣中的所有元素。 示例 1&#xff1a; 輸入&#xff1a;matrix [[1,2,3],[4,5,6],[7,8,9]] 輸出&#xff1a;[1,2,3,6,9,8,7,4,5] 示例 2&#xff1a; 輸入&#xff1a;matrix [[1,…

微服務架構技能

2019獨角獸企業重金招聘Python工程師標準>>> 微服務架構技能 博客分類&#xff1a; 架構 &#xff08;StuQ 微服務技能圖譜&#xff09; 2課程簡介 本課程分為基礎篇和高級篇兩部分&#xff0c;旨在通過完整的案例&#xff0c;呈現微服務的開發、測試、構建、部署、…

phpstorm 調試_PhpStorm中的多用戶調試

phpstorm 調試by Ray Naldo雷納爾多(Ray Naldo) PhpStorm中的多用戶調試 (Multi-User Debugging in PhpStorm) 使用Xdebug和DBGp代理 (Using Xdebug and DBGp Proxy) “Er, wait a minute… Don’t you just use xdebug.remote_connect_back which has been introduced since …

Tableau Desktop認證:為什么要關心以及如何通過

Woah, Tableau!哇&#xff0c;Tableau&#xff01; By now, almost everyone’s heard of the data visualization software that brought visual analytics to the public. Its intuitive drag and drop interface makes connecting to data, creating graphs, and sharing d…

約束布局constraint-layout導入失敗的解決方案 - 轉

今天有同事用到了約束布局&#xff0c;但是導入我的工程出現錯誤 **提示錯誤&#xff1a; Could not find com.Android.support.constraint:constraint-layout:1.0.0-alpha3** 我網上查了一下資料&#xff0c;都說是因為我的androidStudio版本是最新的穩定版導入這個包就會報這…

算法復習:冒泡排序

思想&#xff1a;對于一個列表,每個數都是一個"氣泡 "&#xff0c;數字越大表示"越重 "&#xff0c;最重的氣泡移動到列表最后一位&#xff0c;冒泡排序后的結果就是“氣泡”按照它們的重量依次移動到列表中它們相應的位置。 算法&#xff1a;搜索整個列表…

leetcode 59. 螺旋矩陣 II(遞歸)

給你一個正整數 n &#xff0c;生成一個包含 1 到 n2 所有元素&#xff0c;且元素按順時針順序螺旋排列的 n x n 正方形矩陣 matrix 。 示例 1&#xff1a; 輸入&#xff1a;n 3 輸出&#xff1a;[[1,2,3],[8,9,4],[7,6,5]] 解題思路 按層進行數字的填充&#xff0c;每一層…

前端基礎進階(七):函數與函數式編程

縱觀JavaScript中所有必須需要掌握的重點知識中&#xff0c;函數是我們在初學的時候最容易忽視的一個知識點。在學習的過程中&#xff0c;可能會有很多人、很多文章告訴你面向對象很重要&#xff0c;原型很重要&#xff0c;可是卻很少有人告訴你&#xff0c;面向對象中所有的重…

期權數據 獲取_我如何免費獲得期權數據

期權數據 獲取by Harry Sauers哈里紹爾斯(Harry Sauers) 我如何免費獲得期權數據 (How I get options data for free) 網頁抓取金融簡介 (An introduction to web scraping for finance) Ever wished you could access historical options data, but got blocked by a paywall…

顯示與刪除使用工具

右擊工具菜單欄中的空白處選擇自定義 在彈出的自定義菜單中選擇命令選項在選擇想要往里面添加工具的菜單&#xff0c;之后在選擇要添加的工具 若想要刪除工具欄中的某個工具&#xff0c;在打開自定義菜單后&#xff0c;按住鼠標左鍵拖動要刪除工具到空白處 例如 轉載于:https:/…

js值的拷貝和值的引用_到達P值的底部:直觀的解釋

js值的拷貝和值的引用介紹 (Introduction) Welcome to this lesson on calculating p-values.歡迎參加有關計算p值的課程。 Before we jump into how to calculate a p-value, it’s important to think about what the p-value is really for.在我們開始計算p值之前&#xff…

leetcode 115. 不同的子序列(dp)

給定一個字符串 s 和一個字符串 t &#xff0c;計算在 s 的子序列中 t 出現的個數。 字符串的一個 子序列 是指&#xff0c;通過刪除一些&#xff08;也可以不刪除&#xff09;字符且不干擾剩余字符相對位置所組成的新字符串。&#xff08;例如&#xff0c;“ACE” 是 “ABCDE…