翻译 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!
  这段探索之旅一定是信息量巨大并且充满意义的-让我们尽情享受吧!

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