翻譯 Secrets of the JavaScript Ninja 邊譯邊學(5)

Secrets of the JavaScript Ninja 邊譯邊學(5)

1.4 Best practices
1.4最佳實踐

Mastery of the JavaScript language and a grasp of cross-browser coding issues are important parts of becoming an expert web application developer, but they’re not the complete picture. To enter the Big Leagues you also need to exhibit the traits that scores of previous developers have proved are beneficial to the development of quality code. These traits, which we will examine in depth in chapter 2, are known as best practices and, in addition to mastery of the language, include such elements as:

掌握JavaScript語言和跨瀏覽器編碼問題是成爲一名專業的Web應用程序開發人員非常重要的一部分,但這不是全部。要想進入行業圈,你還需要展示一些優秀的特質,之前大量的開發者已經證明這些特質對於開發高質量代碼非常有益。這些特質(我們稱之爲最佳實踐,將在第二章深入研究),除了掌握語言以外,還包括以下要素:

  • Testing

  • Performance analysis

  • Debugging skills

  • 測試

  • 性能分析

  • 調試技巧

  It is vitally important to adhere to these practices in our coding, and frequently. The complexity of cross-browser development certainly justifies it. Let’s examine a couple of these practices.

  在我們的編碼過程中,經常遵守這些好習慣很重要。開發跨瀏覽器程序的複雜性證明了這點。讓我們檢查一下這些做法的其中幾個:

1.4.1 Best practice: testing
1.4.1 最佳實踐:測試

Throughout this book, we’ll be applying a number of testing techniques that serve to ensure that our example code operates as intended, as well as to serve as examples of how to test general code. The primary tool that we will be using for testing is an assert() function, whose purpose is to assert that a premise is either true or false. The general form of this function is:

在本書中,我們將應用一些測試技術來確我們的示例代碼按預期運行,並作爲展示如何測試通用代碼的示例。我們將要使用的主要工具是assert()函數,其目的是確認一個假設是否正切。該函數的一般形式如下:

assert(condition,message);

where the first parameter is a condition that should be true, and the second is a message that will be raised if it is not.

其中第一個參數是一個條件,該條件本應該返回true,如果它不返回true,那麼第二個參數message會觸發顯示。

Consider, for example:
例如,考慮:

*assert(a == 1, "Disaster! A is not 1!");

If the value of variable a is not equal to one, the assertion fails and the somewhat overly-dramatic) message is raised.

如果變量a的值不等於1,斷言失敗,一條引人注意的消息觸發顯示出來。

Note that the assert() function is not an innate feature of the language (as it is in some other languages, such as Java), so we’ll be implementing it ourselves. We’ll be discussing its implementation and use in chapter 2.

注意函數*assert()*並不是這個語言中的固有函數(儘管在一些其他語言中是固有函數,例如Java),所以我們要自己實現它,我們會在第二章討論它的實現。

1.4.2 Best practice: performance analysis
1.4.2最佳實踐:性能分析

Another important practice is performance analysis. The JavaScript engine in the browsers have been making astounding strides in the performance of JavaScript itself, but that’s no excuse for us to write sloppy and inefficient code. Another function we’ll be implementing and using in this book is the perf() function for collecting performance information.

另一個重要的實踐是性能分析。瀏覽器中JavaScript引擎在JavaScript本身的性能方面已經有了很大的提升,但那不是我們編寫凌亂、低效代碼的理由。在本書中我們將要實現的另一個函數是perf(),它的作用的收集性能相關信息。

An example of its use would be:
該函數使用示例如下:

perf("String Concatenation", function(){
var name = "Fred";
for (var i = 0; i < 20; i++) {
name += name;
}
});

These best-practice techniques, along with the others that we’ll learn along the way, will greatly enhance our JavaScript development. Developing applications with the restricted resources that a browser provides, coupled with the increasingly complex world of browser capability and compatibility, makes having a robust and complete set of skills a necessity.

這些最佳實踐技巧,以及我們將在本書中學習的其他技巧,會大大提高我們的JavaScript開發水平。在資源受限的瀏覽器上開發應用程序,現代瀏覽器在能力和兼容性上的日益複雜化,這些因素使得擁有強大而完善的技能很有必要。

1.5 Summary
1.5 總結

Cross-browser web application development is hard; harder than most people would think.

跨瀏覽器Web應用程序開發很困難,比大多數人想象的更加困難。

  In order to pull it off, we need not only a mastery of the JavaScript language, but a thorough knowledge of the browsers, along with their quirks and inconsistencies, and a good grounding in accepted best practices.

  爲了圓滿完成開發任務,我們不僅需要精通JavaScript語言,還需要對瀏覽器有全面瞭解,包括它們的特性和不一致性,以及擁有公認的最佳實踐的良好基礎。

  While JavaScript development can certainly be challenging, there are those brave souls who have already gone down this torturous route: the developers of JavaScript libraries. We’ll be distilling the knowledge gained during the construction of these code bases, effectively fueling our development skills, and raising them to world class level.

  儘管JavaScript開發肯定具有挑戰性,但是有一些勇敢的靈魂已經走在這條痛苦的道路上了:JavaScript庫函數的開發者們。我們將吸取構建這些基礎代碼過程中獲得的經驗教訓,有效提升我們的開發技術,達到世界一流水平。

  This exploration will certainly be informative and educational – let’s enjoy the ride!
  這段探索之旅一定是信息量巨大並且充滿意義的-讓我們盡情享受吧!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章