翻譯 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方法在第九章講
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章