標籤:javascript 前端 編碼工具 編碼調試
一、前言
工欲善其事,必先利其器。事和器之間是對應的,做什麼事用什麼工具,這是有講究的。
對於JavaScript來說(當然並不僅僅限於JavaScript,其他語言也是如此),我認為學習的工具和生產的工具,還是區別開來會更好一些。在生產中,一切以生產效率為目標,使用功能強大的IDE是不二的選擇。而學習,自動化太高的工具,反而會阻礙你的學習。當然,如果你是一名熟練的工程師,可以不用考慮這些問題。
我的習慣是,當要學習新技術,或者新語言的時候,我喜歡手工代碼,會用一個簡潔、方便的文字編輯器,比如EditPlus編寫代碼,編譯也是用命令列的方式手工進行,這讓我洞察語言或者技術的本質原理,瞭解更多的細節。然後,我會找一個代表性的開源項目,用IDE分析它的原始碼,從而掌握這門語言或者技術是如何在生產環境中解決實際問題的。
二、編碼工具
只要是編輯文本的工具,都可以用以JavaScript編碼。不過,考慮到效能、效率,以及方便性,大家用的比較多的還是EditPlus、UltraEdit、EmEditor、Notepad++、Sublime Text,以及WebStorm、Visual Studio等。後兩個是專業的IDE,屬於重量級開發工具。
EditPlus小巧方便、功能強大,用起來很是順手。一般通過目錄來組織和管理專案檔。
650) this.width=650;" src="https://app.yinxiang.com/shard/s38/res/bf7ba1be-5017-4e82-8f0f-dec5ce2c6116.png" name="bf7ba1be-5017-4e82-8f0f-dec5ce2c6116" class="en-media" style="margin:.857412em 0px 1.286em;padding:0px;border:0px;height:auto;" />
不過EditPlus預設使用IE瀏覽器,而且是”無縫瀏覽器“,開啟的瀏覽器頁面,也在EditPlus的一個子文檔頁面,無法使用瀏覽器的”開發人員工具“,對於複雜JS代碼的調試很不方便。可以通過喜好設定設定來修改瀏覽器的配置。
650) this.width=650;" src="https://app.yinxiang.com/shard/s38/res/e29604d4-f492-4741-8be1-15ff08ff0378.png" name="e29604d4-f492-4741-8be1-15ff08ff0378" class="en-media" style="margin:.857412em 0px 1.286em;padding:0px;border:0px;height:auto;" />
這樣瀏覽網頁的時候可以開啟外部的瀏覽器,按F12進行調試。
650) this.width=650;" src="https://app.yinxiang.com/shard/s38/res/df179664-7ff8-4964-83c1-e701feddf060.png" name="df179664-7ff8-4964-83c1-e701feddf060" class="en-media" style="margin:.857412em 0px 1.286em;padding:0px;border:0px;height:auto;" />
650) this.width=650;" src="https://app.yinxiang.com/shard/s38/res/284e1eaf-8c5c-4618-98c5-225396a247c6.png" class="en-media" style="margin:.857412em 0px 1.286em;padding:0px;border:0px;height:auto;" />
EditPlus是收費軟體,如果你不願意花錢註冊,可以使用Notepad++。Notepad++可以使用工程管理項目。
650) this.width=650;" src="https://app.yinxiang.com/shard/s38/res/d266bcef-a56c-4ca4-848b-ad566d994aba.png" name="d266bcef-a56c-4ca4-848b-ad566d994aba" class="en-media" style="margin:.857412em 0px 1.286em;padding:0px;border:0px;height:auto;" />
650) this.width=650;" src="https://app.yinxiang.com/shard/s38/res/32f6f5d5-6657-4e19-81fd-dc74ec3843a2.png" class="en-media" style="margin:.857412em 0px 1.286em;padding:0px;border:0px;height:auto;" />
然後可以"從目錄添加檔案",將你的專案檔添加到工程,方便管理。
650) this.width=650;" src="https://app.yinxiang.com/shard/s38/res/641b25fa-681e-440d-bba3-b3fabb80b7e3.png" class="en-media" style="margin:.857412em 0px 1.286em;padding:0px;border:0px;height:auto;" />
當然,別忘了儲存工作區。
650) this.width=650;" src="https://app.yinxiang.com/shard/s38/res/1f905f11-a327-419e-adc7-6bded808a393.png" class="en-media" style="margin:.857412em 0px 1.286em;padding:0px;border:0px;height:auto;" />
可以通過快速鍵開啟設定好的瀏覽器瀏覽頁面、偵錯工具。你可以修改運行瀏覽器的快速鍵,以符合你的個人習慣。
650) this.width=650;" src="https://app.yinxiang.com/shard/s38/res/b74ffa1c-9064-4ca9-b112-346f94187a97.png" name="b74ffa1c-9064-4ca9-b112-346f94187a97" class="en-media" style="margin:.857412em 0px 1.286em;padding:0px;border:0px;height:auto;" />
650) this.width=650;" src="https://app.yinxiang.com/shard/s38/res/c166c398-c28a-4074-837c-199f00bae3f4.png" name="c166c398-c28a-4074-837c-199f00bae3f4" class="en-media" style="margin:.857412em 0px 1.286em;padding:0px;border:0px;height:auto;" />
Sublime Text是一個功能強大的代碼編輯器,是收費軟體,但可以無限期試用。Sublime Text可以通過外掛程式擴充、增強功能,所以功能強大,而且很好玩,深受程式員喜愛。安裝外掛程式的方法主要有兩個。
安裝方法一:直接安裝。
可以直接下載外掛程式安裝包,解壓縮到Packages目錄。Packages目錄可以通過菜單->preferences->browse packages查看。
650) this.width=650;" src="https://app.yinxiang.com/shard/s38/res/fff2ce12-2433-4c24-ba22-068f9abe87dc.png" name="fff2ce12-2433-4c24-ba22-068f9abe87dc" class="en-media" style="margin:.857412em 0px 1.286em;padding:0px;border:0px;height:auto;" />
650) this.width=650;" src="https://app.yinxiang.com/shard/s38/res/71cfec65-1017-4d63-8554-500881e47f6d.png" class="en-media" style="margin:.857412em 0px 1.286em;padding:0px;border:0px;height:auto;" />
安裝方法二:使用Package Control組件安裝。
通過菜單->Perferences->package control,調出Install Package,然後在列表中選中要安裝的外掛程式。
650) this.width=650;" src="https://app.yinxiang.com/shard/s38/res/1dd3a3f8-a941-4677-9695-d07840df3bd5.png" class="en-media" style="margin:.857412em 0px 1.286em;padding:0px;border:0px;height:auto;" />
650) this.width=650;" src="https://app.yinxiang.com/shard/s38/res/733a61e5-f53c-4b10-8c02-afa78c4370da.png" class="en-media" style="margin:.857412em 0px 1.286em;padding:0px;border:0px;height:auto;" />
650) this.width=650;" src="https://app.yinxiang.com/shard/s38/res/5de820bf-28e4-49bb-82a0-216b0f7ad003.png" class="en-media" style="margin:.857412em 0px 1.286em;padding:0px;border:0px;height:auto;" />
比如說,我們希望在Sublime Text工作環境中可以直接開啟瀏覽器,以查看網頁的效果進行調試,可以安裝一款很實用的右鍵菜單增強外掛程式SideBarEnhancements。在Sublime Text左側FOLDERS欄中的右鍵菜單功能就會增強很多。
該外掛程式還能讓我們自訂快速鍵設定使用哪款瀏覽器,點擊功能表列的preferences->package setting->side bar->Key Building-User,進行設定。
650) this.width=650;" src="https://app.yinxiang.com/shard/s38/res/b4fb9a1c-f1cc-4090-a544-fbced8cb8ed7.png" name="b4fb9a1c-f1cc-4090-a544-fbced8cb8ed7" class="en-media" style="margin:.857412em 0px 1.286em;padding:0px;border:0px;height:auto;" />
650) this.width=650;" src="https://app.yinxiang.com/shard/s38/res/0a5db663-7e2e-4fce-8393-0d7de4eb9003.png" class="en-media" style="margin:.857412em 0px 1.286em;padding:0px;border:0px;height:auto;" />
這樣,你就可以直接通過右鍵菜單,或者快速鍵開啟瀏覽器來查看網頁了。
650) this.width=650;" src="https://app.yinxiang.com/shard/s38/res/121f4216-b686-4863-9012-9c1b668056f6.png" class="en-media" style="margin:.857412em 0px 1.286em;padding:0px;border:0px;height:auto;" />
安裝SublimeTmpl外掛程式可以快速組建檔案模板,比如快速鍵“ctrl+alt+h”產生HTML檔案模板。
650) this.width=650;" src="https://app.yinxiang.com/shard/s38/res/35a00802-829e-4442-a4ef-6298d3e33c14.png" class="en-media" style="margin:.857412em 0px 1.286em;padding:0px;border:0px;height:auto;" />
安裝better completion外掛程式,支援Javascript、JQuery、Twitter Bootstrap架構、HTML5標籤屬性提示。
650) this.width=650;" src="https://app.yinxiang.com/shard/s38/res/ab527430-f741-499c-815a-934e8df9ebb5/11.gif" name="ab527430-f741-499c-815a-934e8df9ebb5" class="en-media" style="margin:.857412em 0px 1.286em;padding:0px;border:0px;height:auto;" />
注意設定檔,首先將preferences->package setting->sublime better completion->Settings-Default中的配置內容複寫到preferences->package setting->sublime better completion->Settings-User中,然後進行修改。
比如,現在啟用JQuery自動完成,則修改“"jquery": false,”為“"jquery": true,”。
650) this.width=650;" src="https://app.yinxiang.com/shard/s38/res/c2558a1a-d97e-426e-9146-9b818b108520.png" name="c2558a1a-d97e-426e-9146-9b818b108520" class="en-media" style="margin:.857412em 0px 1.286em;padding:0px;border:0px;height:auto;" />
通過配置,可以在Sublime Text內運行javascript,不用開啟外部瀏覽器進行瀏覽調試,查看代碼運行效果。
第一步:安裝Node.js
安裝完後,要看下是否配置好了nodejs的環境變數。
650) this.width=650;" src="https://app.yinxiang.com/shard/s38/res/c780b036-7ee5-455a-b443-6b8b3cc4fe05.png" name="c780b036-7ee5-455a-b443-6b8b3cc4fe05" class="en-media" style="margin:.857412em 0px 1.286em;padding:0px;border:0px;height:auto;" />
然後,在命令列中輸入node指令測試一下。
650) this.width=650;" src="https://app.yinxiang.com/shard/s38/res/8002bdea-9cb0-4ec4-97c1-d55e09fd6c48.png" class="en-media" style="margin:.857412em 0px 1.286em;padding:0px;border:0px;height:auto;" />
第二步:添加build system
點擊菜單Tools -> Build System -> New Build System,輸入配置資訊並儲存。
650) this.width=650;" src="https://app.yinxiang.com/shard/s38/res/f8116cf0-b976-4941-af93-1c5f95870fc1.png" name="f8116cf0-b976-4941-af93-1c5f95870fc1" class="en-media" style="margin:.857412em 0px 1.286em;padding:0px;border:0px;height:auto;" />
650) this.width=650;" src="https://app.yinxiang.com/shard/s38/res/a501a8eb-c85e-456a-8dbd-7237b32c8445.png" name="a501a8eb-c85e-456a-8dbd-7237b32c8445" class="en-media" style="margin:.857412em 0px 1.286em;padding:0px;border:0px;height:auto;" />
650) this.width=650;" src="https://app.yinxiang.com/shard/s38/res/f4cfb522-c2ef-4481-b6c8-4fbcd6372bae.png" name="f4cfb522-c2ef-4481-b6c8-4fbcd6372bae" class="en-media" style="margin:.857412em 0px 1.286em;padding:0px;border:0px;height:auto;" />
然後,可以在Sublime Text內直接運行JavaScript了。
650) this.width=650;" src="https://app.yinxiang.com/shard/s38/res/aa5fb512-1ada-4e68-8926-43ae817c3922.png" name="aa5fb512-1ada-4e68-8926-43ae817c3922" class="en-media" style="margin:.857412em 0px 1.286em;padding:0px;border:0px;height:auto;" />
三、代碼調試
沒有誰寫代碼能夠一蹴而就。驗證思路、追蹤bug、解決錯誤,調試伴隨著我們編碼的全過程。好的調試方法和工具,可以極大地提高我們的編碼效率。
我們一般在瀏覽器中運行我們的代碼,可以通過提示資訊,比如alert語句、console.log語句,來驗證輸出結果是否和我們預想的一致。也可以用F12調出瀏覽器的開發人員工具,對代碼進行斷點調試。
比如,有一個函數sayHello,我們要調試它。
function sayHello(name){
var msg = "你好,"+name;
console.log(msg);
}
我們用EditPlus編寫了兩個檔案。
test.js
650) this.width=650;" src="https://app.yinxiang.com/shard/s38/res/beb3f27a-99c4-4fb5-a750-64187908e221.png" name="beb3f27a-99c4-4fb5-a750-64187908e221" class="en-media" style="margin:.857412em 0px 1.286em;padding:0px;border:0px;height:auto;" />
test.html
650) this.width=650;" src="https://app.yinxiang.com/shard/s38/res/5ce6e482-48b7-435c-8f75-fd8a8cb15696.png" class="en-media" style="margin:.857412em 0px 1.286em;padding:0px;border:0px;height:auto;" />
根據前面講的,我們可以設定EditPlus調用外部瀏覽器開啟HTML頁面,並通過快速鍵F12調出瀏覽器的開發人員工具,重新整理頁面,我們在控制台看到了程式的輸出結果。
650) this.width=650;" src="https://app.yinxiang.com/shard/s38/res/3f935de9-dc4b-4381-b191-0b8243ef315d.png" class="en-media" style="margin:.857412em 0px 1.286em;padding:0px;border:0px;height:auto;" />
你可以點擊“Source”查看原始碼,並通過滑鼠點擊添加斷點,重新整理頁面重新載入網頁,就可以進行斷點調試了。
650) this.width=650;" src="https://app.yinxiang.com/shard/s38/res/66fb6d26-3fda-42fc-80c4-6cdffe492407.png" class="en-media" style="margin:.857412em 0px 1.286em;padding:0px;border:0px;height:auto;" />
當然,功能強大的IDE,將編碼、調試整合在了一起,用起來就很方便。方便是有代價的,這些工具為你的生產提高了效率,你就要交出你的money了。WebStorm、Visual Studio等,就是這樣的工具。這裡講一下功能最強大,用起來最方便的Visual Studio,其他的工具,你可以根據自己的喜好,選擇學習和使用。
首先建立一個網站項目。簡單明了起見,我們建立一個空項目,然後把剛才那兩個檔案複製到工程中。
650) this.width=650;" src="https://app.yinxiang.com/shard/s38/res/28719b13-4e10-4089-8f20-6587b527821d.png" class="en-media" style="margin:.857412em 0px 1.286em;padding:0px;border:0px;height:auto;" />
我們這次直接把斷點加在網頁中。
650) this.width=650;" src="https://app.yinxiang.com/shard/s38/res/077392c2-c804-4b55-85ee-51455fd909ff.png" class="en-media" style="margin:.857412em 0px 1.286em;padding:0px;border:0px;height:auto;" />
運行程式,就會停在斷點的地方。
650) this.width=650;" src="https://app.yinxiang.com/shard/s38/res/db3f9b7d-85b9-4e4a-a6d5-ff0ce9109b1d.png" name="db3f9b7d-85b9-4e4a-a6d5-ff0ce9109b1d" class="en-media" style="margin:.857412em 0px 1.286em;padding:0px;border:0px;height:auto;" />
逐步運行,查看程式運行結果。
650) this.width=650;" src="https://app.yinxiang.com/shard/s38/res/c623a7e9-d561-40a4-bfbc-503835d71330.png" name="c623a7e9-d561-40a4-bfbc-503835d71330" class="en-media" style="margin:.857412em 0px 1.286em;padding:0px;border:0px;height:auto;" />
四、總結
JavaScript,本是一種為Web瀏覽器設計的指令碼語言,但近幾年,已經廣泛應用於各種軟體應用中。不僅有運行在服務端的JavaScript代碼,也有了用來驅動iOS和Android應用的JavaScript代碼,甚至還有的用來控制機器人。因此用什麼樣的工具好,要根據自己的工作情境和需求,選擇適合的工具。
本文出自 “老惠” 部落格,轉載請與作者聯絡!
第一節:JavaScript編碼和調試