javascript控制臺_如何使用JavaScript控制臺改善工作流程

javascript控制臺

by Riccardo Canella

里卡多·卡內拉(Riccardo Canella)

如何使用JavaScript控制臺改善工作流程 (How you can improve your workflow using the JavaScript console)

As a web developer, you know very well the need to debug your code. We often use external libraries for logs, and to format and/or display them in some cases, but the console of our browsers is much more powerful than we think.

作為Web開發人員,您非常了解調試代碼的需求。 我們經常使用外部庫來記錄日志,并在某些情況下格式化和/或顯示它們,但是瀏覽器的控制臺功能比我們想象的要強大得多。

When we think about the console, the first thing that comes to mind and the console.log, right? But there are many more methods than those we imagine. Now we will see how to make the most of using the console, and I’ll give you some tips to make them these methods more readable

當我們想到控制臺時,首先想到的是console.log ,對吧? 但是有比我們想象的更多的方法。 現在,我們將了解如何充分利用控制臺,并且我將為您提供一些技巧,以使這些方法更具可讀性

什么是控制臺? (What is the Console?)

The JavaScript console is a built-in feature in modern browsers that comes with out-of-the-box development tools in a shell-like interface. It allows a developer to:

JavaScript控制臺是現代瀏覽器中的內置功能,它在類似Shell的界面中帶有開箱即用的開發工具。 它允許開發人員執行以下操作:

  • View a log of errors and warnings that occur on a web page.

    查看網頁上出現的錯誤和警告日志。
  • Interact with the web page using JavaScript commands.

    使用JavaScript命令與網頁進行交互。
  • Debug applications and traverse the DOM directly in the browser.

    調試應用程序并直接在瀏覽器中遍歷DOM。
  • Inspect and analyze network activity

    檢查并分析網絡活動

Basically, it empowers you to write, manage, and monitor JavaScript right within your browser.

基本上,它使您可以直接在瀏覽器中編寫,管理和監視JavaScript。

Console.log,Console.error,Console.warn和Console.info (Console.log, Console.error, Console.warn and Console.info)

These are probably the most used methods of all. You can pass more than one parameter to these methods. Each parameter is evaluated and concatenated in a string delimited by the space, but in case of objects or arrays you can navigate between their properties.

這些可能是最常用的方法。 您可以將多個參數傳遞給這些方法。 每個參數都以空格分隔的字符串進行評估和連接,但是對于對象或數組,您可以在其屬性之間導航。

Console.group (Console.group)

This method allows you to group a series of console.logs (but also error info, and so on) under a group that can be collapsed. The syntax is really very simple: just enter all the console.log we want to group before a console.group() (or console.groupCollapsed() if we want it to be closed by default). Then add a console.groupEnd() at the end to close the group.

通過此方法,您可以將一系列console.logs(還包括錯誤信息,等等)歸為一個可折疊的組。 語法真的很簡單:只需輸入所有console.log一個之前我們要組console.group()console.groupCollapsed()如果我們希望它在默認情況下關閉)。 然后在末尾添加console.groupEnd()以關閉組。

The results will look like this:

結果將如下所示:

控制臺表 (Console.table)

Since I discovered the console.table my life has changed. Displaying JSON or very large JSON arrays inside a console.log is a terrifying experience. The console.table allows us to visualize these structures inside a beautiful table where we can name the columns and pass them as parameters.

自從發現console.table我的生活發生了變化。 在console.log顯示JSON或非常大的JSON數組是一種令人恐懼的體驗。 console.table允許我們在漂亮的表中可視化這些結構,我們可以在其中命名列并將它們作為參數傳遞。

The result is wonderful and very useful in debugging:

結果是極好的,并且在調試中非常有用:

Console.count,Console.time和Console.timeEnd (Console.count, Console.time and Console.timeEnd)

These three methods are the Swiss army knife for every developer who needs to debug. The console.count counts and outputs the number of times that count() has been invoked on the same line and with the same label. The console.time starts a timer with a name specified as an input parameter, and can run up to 10,000 simultaneous timers on a given page. Once initiated, we use a call to console.timeEnd to stop the timer and print the elapsed time to the Console.

對于需要調試的每個開發人員,這三種方法都是瑞士軍刀。 console.count計數并輸出在同一行上使用相同標簽調用count()的次數。 console.time以指定為輸入參數的名稱啟動計時器,并且在給定頁面上最多可以同時運行10,000個計時器。 啟動后,我們將使用對console.timeEnd的調用來停止計時器并將經過的時間打印到控制臺。

The output will look like this:

輸出將如下所示:

Console.trace和Console.assert (Console.trace and Console.assert)

These methods simply print a stack trace from the point where it was called. Imagine you are creating a JS library and want to inform the user where the error was generated. In that case, these methods can be very useful. The console.assert is like the console.trace but will print only if the condition passed didn’t pass.

這些方法只是從調用點打印堆棧跟蹤。 假設您正在創建一個JS庫,并且想通知用戶錯誤發生的位置。 在這種情況下,這些方法可能非常有用。 console.assert類似于console.trace但僅在傳遞的條件未通過時才會打印。

As we can see, the output is exactly what React (or any other library) would show us when we generate an exception.

如我們所見,輸出正是生成異常時React(或任何其他庫)向我們顯示的內容。

刪除所有控制臺? (Delete all the Consoles ?)

Using consoles often forces us to eliminate them. Or sometimes we forget about the production build (and only notice them by mistake days and days later). Of course, I do not advise anyone to abuse consoles where they are not needed (the console in the change input handle can be deleted after you see that it works). You should leave error logs or trace logs in development mode to help you debug. I use Webpack a lot, both at work and in my own projects. This tool allows you to delete all the consoles that you do not want to remain (by type) from the production build using the uglifyjs-webpack-plugin ?

使用控制臺通常會迫使我們消除它們。 有時我們會忘記生產版本(并且幾天后才錯誤地注意到它們)。 當然,我不建議任何人濫用不需要控制臺的控制臺(在看到更改輸入句柄后可以刪除控制臺)。 您應該將錯誤日志或跟蹤日志留在開發模式下,以幫助您調試。 無論是在工作中還是在我自己的項目中,我都經常使用Webpack。 使用此工具,您可以使用uglifyjs-webpack-plugin從生產版本中刪除所有不想保留的控制臺(按類型)。

const UglifyJsPlugin = require('uglifyjs-webpack-plugin')var debug = process.env.NODE_ENV !== "production";.....optimization: {        minimizer: !debug ? [            new UglifyJsPlugin({                // Compression specific options                uglifyOptions: {                    // Eliminate comments                    comments: false,                    compress: {                       // remove warnings                       warnings: false,                       // Drop console statements                       drop_console: true                    },                }           })] : []}

The configuration is really trivial and it simplifies the work, so have fun with the console (but do not abuse it!)

配置確實很簡單,并且簡化了工作,因此可以在控制臺上玩樂(但不要濫用它!)

If you liked the article please clap and follow me :)

如果您喜歡這篇文章,請鼓掌并關注我:)

Thx and stay tuned ?

謝謝,敬請期待?

Follow my last news and tips on Facebook

在Facebook上關注我的最新消息和提示

翻譯自: https://www.freecodecamp.org/news/how-you-can-improve-your-workflow-using-the-javascript-console-bdd7823a9472/

javascript控制臺

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

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

相關文章

appium===setup/setupclass的區別,以及@classmathod的使用方法

一、裝飾器 1.用setUp與setUpClass區別 setup():每個測試case運行前運行 teardown():每個測試case運行完后執行 setUpClass():必須使用classmethod 裝飾器,所有case運行前只運行一次 tearDownClass():必須使用classmethod裝飾器,所有case運行完后只運行一次 2.是修飾符&#xf…

cache failed module status_Flutter混編之路——iOS踩坑記錄

一、運行Xcode編譯或者flutter run/build 過程中報錯:"x86_64" is not an allowed value for option "ios-arch".解決方案在Debug.xcconfig中指定 “FLUTTER_BUILD_MODEdebug”,Release.xcconfig中指定“FLUTTER_BUILD_MODErelease”…

【最短路徑Floyd算法詳解推導過程】看完這篇,你還能不懂Floyd算法?還不會?...

簡介 Floyd-Warshall算法(Floyd-Warshall algorithm),是一種利用動態規劃的思想尋找給定的加權圖中多源點之間最短路徑的算法,與Dijkstra算法類似。該算法名稱以創始人之一、1978年圖靈獎獲得者、斯坦福大學計算機科學系教授羅伯特…

java object類的常用子類_Java中Object類常用的12個方法,你用過幾個?

前言Java 中的 Object 方法在面試中是一個非常高頻的點,畢竟 Object 是所有類的“老祖宗”。Java 中所有的類都有一個共同的祖先 Object 類,子類都會繼承所有 Object 類中的 public 方法。先看下 Object 的類結構(快捷鍵:alt7):1.…

leetcode面試題 04.12. 求和路徑(dfs)

給定一棵二叉樹,其中每個節點都含有一個整數數值(該值或正或負)。設計一個算法,打印節點數值總和等于某個給定值的所有路徑的數量。注意,路徑不一定非得從二叉樹的根節點或葉節點開始或結束,但是其方向必須向下(只能從父節點指向子…

javaweb學習總結(二十二)——基于Servlet+JSP+JavaBean開發模式的用戶登錄注冊

一、ServletJSPJavaBean開發模式(MVC)介紹 ServletJSPJavaBean模式(MVC)適合開發復雜的web應用,在這種模式下,servlet負責處理用戶請求,jsp負責數據顯示,javabean負責封裝數據。 ServletJSPJavaBean模式程序各個模塊之間層次清晰&…

2018黃河獎設計大賽獲獎_宣布我們的freeCodeCamp 2018杰出貢獻者獎獲獎者

2018黃河獎設計大賽獲獎by Quincy Larson昆西拉爾森(Quincy Larson) 宣布我們的freeCodeCamp 2018杰出貢獻者獎獲獎者 (Announcing Our freeCodeCamp 2018 Top Contributor Award Winners) Over the past 3 years, freeCodeCamp.org has grown from a small open source proje…

Log4j配置詳解

來自: http://www.blogjava.net/zJun/archive/2006/06/28/55511.html Log4J的配置文件(Configuration File)就是用來設置記錄器的級別、存放器和布局的,它可接keyvalue格式的設置或xml格式的設置信息。通過配置,可以創建出Log4J的運行環境。1. 配置文件 …

cors數據類型_如何根據RTK的差分格式選擇千尋cors賬號的源節點進行設置?

千尋cors賬號的設置中源節點是根據使用的品牌RTK是為雙星儀器還是三星儀器選擇,但問題就在于我們看到的RTK的技術參數中一般很少見到標注儀器的衛星系統,更多的是差分格式。其實千尋cors賬號的源節點也可以根據RTK的差分格式進行選擇,不過這兩…

java swing 串口_ComTest 接收串口數據,并顯示在文本框內,通過JavaSwing實現 Develop 265萬源代碼下載- www.pudn.com...

文件名稱: ComTest下載 收藏√ [5 4 3 2 1 ]開發工具: Java文件大小: 3157 KB上傳時間: 2016-09-21下載次數: 0提 供 者: 韓坤詳細說明:接收串口數據,并顯示在文本框內,通過JavaSwing實現-Receive serial data, and displayed in the t…

leetcode329. 矩陣中的最長遞增路徑(dfs)

給定一個整數矩陣,找出最長遞增路徑的長度。對于每個單元格,你可以往上,下,左,右四個方向移動。 你不能在對角線方向上移動或移動到邊界外(即不允許環繞)。示例 1:輸入: nums [[9,9,4],[6,6,8…

SQL大圣之路筆記——PowerDesigner之新建table、view、proc

1. 新建table、view、proc 轉載于:https://www.cnblogs.com/allenzhang/p/6305564.html

用python繪制一條直線_python繪制直線的方法

本文實例為大家分享了python繪制直線的具體代碼&#xff0c;供大家參考&#xff0c;具體內容如下#!/usr/bin/env pythonimport vtk# 繪制通用方法def myshow(linepolydata):# Now well look at it.lineMapper vtk.vtkPolyDataMapper()if vtk.VTK_MAJOR_VERSION < 5:lineMap…

測試驅動開發 測試前移_我如何以及為什么認為測試驅動開發值得我花時間

測試驅動開發 測試前移by Ronauli Silva通過羅納利席爾瓦(Ronauli Silva) I first read about test driven development (TDD) in some technical reviews blog, but I barely read it (or thought about it). Why would people write tests first when they already knew the…

P2921 [USACO08DEC]在農場萬圣節Trick or Treat on the Farm

對于一個牛&#xff0c;它存在兩種狀態&#xff1a;1.處于聯通分量 2.不處于聯通分量。對于處于聯通分量的牛&#xff0c;求出聯通分量的大小&#xff1b;對于不處于聯通分量的牛&#xff0c;求出其距離聯通分量的路程聯通分量大小。 不同的聯通分量&#xff0c;染上不同的顏色…

ASP.NET MVC5+EF6+EasyUI 后臺管理系統(1)-前言與目錄(持續更新中...)

開發工具&#xff1a;VS2015(2012以上)SQL2008R2以上數據庫 您可以有償獲取一份最新源碼聯系QQ:729994997 價格 666RMB 升級后界面效果如下&#xff1a; 日程管理 http://www.cnblogs.com/ymnets/p/7094914.html 任務調度系統界面 http://www.cnblogs.com/ymnets/p/5065154.h…

leetcode106. 從中序與后序遍歷序列構造二叉樹(dfs)

根據一棵樹的中序遍歷與后序遍歷構造二叉樹。注意: 你可以假設樹中沒有重復的元素。例如&#xff0c;給出中序遍歷 inorder [9,3,15,20,7] 后序遍歷 postorder [9,15,7,20,3] 返回如下的二叉樹&#xff1a;3/ \9 20/ \15 7解題思路 根據后序遍歷的最后一個元素是父節點&…

【FRDM-K64F學習筆記】使用ARM mbed和Keil MDK下載你的第一個程序

FRDM-K64F開發平臺采用MK64FN1M0VLL12微控制器。該控制器包含一個帶有浮點單元的ARM Cortex-M4內核。其最高工作頻率為120MHz&#xff0c;具有256KB的RAM、1MB閃存以及許多其他外設。它非常適合大多數可以采用以太網、SD卡存儲以及板載模擬-數字轉換器的IoT應用。但是&#xff…

php 實時更新內容_億級視頻內容如何實時更新?優酷視頻背后的技術揭秘

簡介&#xff1a; 優酷視頻內容數據天然呈現巨大的網絡結構&#xff0c;各類數據實體連接形成了數十億頂點和百億條邊的數據量&#xff0c;面對巨大的數據量&#xff0c;傳統關系型數據庫往往難以處理和管理&#xff0c;圖數據結構更加貼合優酷的業務場景&#xff0c;圖組織使用…

ios集成firebase_如何使用Firebase將Google Login集成到Ionic應用程序中

ios集成firebaseby Ryan Gordon通過瑞安戈登(Ryan Gordon) 如何使用Firebase將Google Login集成到Ionic應用程序中 (How to integrate Google Login into an Ionic app with Firebase) A lot of apps these days need to maintain some form of user authentication. This hel…