翻译 Secrets of the JavaScript Ninja 边译边学(3)

#Secrets of the JavaScript Ninja 边译边学(3)

1.2 Understanding the JavaScript Language
1.2理解JavaScript语言

  Many JavaScript coders, as they advance through their careers, may get to the point at which they’re actively using the vast array of elements comprising the language: including objects and functions, and, if they’ve been paying attention to coding trends, even anonymous inline functions, throughout their code. In many cases, however, those skills may not be taken beyond fundamental skill levels. Additionally there is generally a very poor understanding of the purpose and implementation of closures in JavaScript, which fundamentally and irrevocably binds the importance of functions to the language.

  对于许多JavaScript编码人员,随着他们职业的晋升,可能会达到一个新的水平:需要经常在代码中使用构成这门语言的大量元素,这些元素包括对象、方法,甚至还有匿名内联函数(如果他们长期关注行业趋势的话)。然而,多数情况下,开发者只是运用了这些元素的基础技能。此外,大多数软件开发者对于JavaScript中闭包的用途和实现的理解非常局限,而闭包毫无疑问从根本上决定了函数在这门语言中的重要性。

  JavaScript consists of a close relationship between objects, functions – which in JavaScript are first class elements – and closures. Understanding the strong relationship between these three concepts vastly improves our JavaScript programming ability, giving us a strong foundation for any type of application development.

  JavaScript中的对象、方法和闭包三者形成密切联系,其中方法是JavaScript的第一要素。理解这三个概念之间的强大关联性能够很大程度上提高我们的JavaScript编码水平,为我们开发任何类型的应用奠定坚实的基础。
图1.1JavaScript中的对象、方法和闭包三者形成密切联系
  Many JavaScript developers, especially those coming from an object-oriented background, may pay a lot of attention to objects, but at the expense of understanding how functions and closures contribute to the big picture.

  许多JavaScript编码人员,尤其是那些有面向对象编程背景的人,可能会更加关注对象,但是花精力了解函数和闭包是如何工作的,会更有助于从整体上理解JavaScript。

  In addition to these fundamental concepts, there are two features in JavaScript that are woefully underused: timers and regular expressions. These two concepts have applications in virtually any JavaScript code base, but aren’t always used to their full potential due to their misunderstood nature.

  可悲的是,除了这些基本概念以外,JavaScript中有以下两个元素并没有得到充分利用:计时器和正则表达式。在几乎所有JavaScript底层代码中,这两个概念都有应用,但是由于人们对其性质的误解,这两个元素都没有充分发挥作用。

  A firm grasp of how timers operate within the browser, all too frequently a mystery, gives us the ability to tackle complex coding tasks such as long-running computations and smooth animations. And an advanced of how regular expressions work allows us to simplify what would otherwise be quite complicated pieces of code.

  完全领悟计时器在浏览器中如何工作(这通常是个谜),使我们能够处理复杂的编码任务,例如:长时间运行的计算和流畅的动画。正则表达式的高级应用允许我们简化原本非常复杂的代码片段。

  As another high point of our advanced tour of the JavaScript language, we’ll take a look at the with statement later on in chapter 10, and the crucially important eval() method in chapter 9.

  作为JavaScript语言高级教程的另一个亮点,我们将在稍后的第十章节介绍With语句,在第九章介绍及其重要的eval()方法。

  All too often these two important language features are trivialized, misused, and even condemned outright by many JavaScript programmers. But by looking at the work of some of the best JavaScript coders we can see that, when used appropriately, these useful features allow for the creation of some fantastic pieces of code that wouldn’t be otherwise possible. To a large degree they can also be used for some interesting meta-programming exercises, molding JavaScript into whatever we want it to be.

  许多JavaScript程序员常常忽视、误用甚至直接谴责这两个重要的语言特性。但是,查阅一些最优秀的JavaScript编码者的工作成果,我们不难发现,如果使用得当,这些有用的特性允许创建一些在其他方面不可能实现的奇妙代码片段。在很大程度上,它们还可以用于一些有趣的元编程练习,JavaScript塑造成我们想要的任何东西。

  Learning how to use these features responsibly and to their best advantage can certainly elevate your code to higher levels.

  学习如何负责任地使用这些特性,并尽其最大的优势,毫无疑问可以将你们的代码提升到更高的级别。

  Honing our skills to tie these concepts and features together gives us a level of understanding that puts the creation of any type of JavaScript application within our reach, and gives us a solid base for moving forward starting with writing solid, cross-browser code.

  将这些概念和特性联系在一起,磨练我们的技能,可以将我们的理解力提高到一个更高的水平,使我们能够在力所能及的范围内创建任何类型的javascript应用程序,并为我们以后编写可靠的跨浏览器代码奠定坚实的基础。

总结
这一节主要讲:

  • 函数、对象、闭包互相紧密联系,需要理解这个联系
  • 计时器、正则表达式两个特性很重要,经常被忽视,要深入理解这两个特性可以使我们创造出更多优秀代码;
  • with语句在第10章讲,eval方法在第九章讲
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章