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

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

回憶總結部分

  從開始翻譯這本書到現在大概有一個多星期了,已經翻譯完了前兩章,感覺比較吃力,但是總體還是覺得比較有益處,因爲需要翻譯,所以很多平時不注意的細節在這裏要仔細多推敲一下。希望後面還會繼續堅持下去。

  第一章主要介紹了JavaScript主要的基本概念,函數、閉包、對象
  第二章主要介紹瞭如何自己構建JavaScript測試工具

  按照簡介中講述的,第三章應該正式開始介紹JavaScript主要概念。

  回憶完畢,開始第三章的翻譯和學習。

開始第三章的學習

3 Functions are fundamental
3 函數是基礎

In this chapter:

  • Why understanding functions is so crucial
  • How functions are first-class objects
  • How the browser invokes function
  • Declaring functions
  • The secrets of how functions are invoked
  • The context within a function

在本章講講述:

  • 爲什麼理解函數如此重要
  • 爲什麼函數是第一類對象
  • 瀏覽器如何觸發函數
  • 聲明函數
  • 觸發函數的祕密
  • 函數上下文

You might have been somewhat surprised, upon turning to this first page of the part of this book dedicated to JavaScript fundamentals, to see that the topic of discussion is to be functions rather than objects.
當你翻到這本書中專門介紹JavaScript基礎知識的第一頁時,你可能會有點驚訝,因爲你會發現討論的主題是函數而不是對象。

  We’ll certainly be paying plenty of attention to objects (particularly in chapter 6), but when it comes down to brass tacks, the main difference between writing JavaScript code like the average Joe (or Jill) and writing it like a JavaScript Ninja, is understand JavaScript as a functional language. The level of the sophistication of all the code that you will ever write in JavaScript hinges upon this realization.
  我們當然會非常關注對象(特別是在第6章中),但歸根結底,像普通的程序員joe(或jill)那樣編寫javascript代碼和像javascript忍者那樣編寫代碼之間的主要區別在於後者將javascript理解爲一種功能性語言。所有你將編寫的JavaScript代碼的水平取決於你對這點的理解。

  If you’re reading this book, you’re not a rank beginner and we’re assuming that you know enough object fundamentals to get by for now (and we’ll be taking a look at more advanced object concepts in chapter 6), but really understanding functions in JavaScript is the single most important weapon we can wield. So important, in fact, that this and the following two chapters are going to be devoted to thoroughly understanding functions in JavaScript.
  如果你正在讀這本書,那麼你不是一個初學者,我們假設你目前已經掌握了足夠多的對象基礎知識(我們將在第6章中介紹更高級的對象概念),但是真正理解javascript中的函數是我們能使用的唯一最重要的武器。實際上,它非常重要,所以我們將會在本章和接下來的兩個章節都致力於深入講解JavaScript的函數。

  Most importantly, in JavaScript, functions are first-class objects; that is, they coexist with, and can be treated like, any other JavaScript object. Just like the more mundane JavaScript data types, they can be referenced by variables, declared with literals, and even passed as function parameters.
  最重要的是,在javascript中,函數是第一類對象;也就是說,它們與任何其他javascript對象共存,並且可以像使用任何其他javascript對象那樣使用函數。就像更普通的javascript數據類型一樣,它們可以被變量引用,用文本聲明,甚至作爲函數參數傳遞。

  The fact that JavaScript treats functions as first-class objects is going to be important on a number of levels, but one significant advantage comes in the form of code terseness. To take a sneak-peek ahead to some code that we’ll examine in greater depth in section 3.1.2, some imperative code (in Java) to perform a collection sort could be:
  從很多層面上來看,JavaScript將函數視爲第一類要素會顯得格外重要,但一個顯著的優勢在於代碼簡潔性。我們不妨先睹爲快,看看在3.1.2節將要深入剖析的一些代碼,一些強類型語言(比如Java)實現集合排序的程序如下所示:

Arrays.sort(values,new Comparator<Integer>(){
public int compare(Integer value1, Integer value2) {
return value2 - value1;
}
});

  The JavaScript equivalent written using a functional approach:
  使用函數方法編寫的等價的JavaScript如下所示:

values.sort(function(value1,value2){ return value2 - value1; });

  Don’t be too concerned if the notation seems odd – you’ll be an old hand at it by the end of this chapter. We just wanted to give you a glimpse of one of the advantages that understanding JavaScript as a functional language will bring to the table.
  如果符號看起來很奇怪的話,不要太擔心——到本章結束時,你將對此完全理解。我們只是想讓您大致瞭解一下將JavaScript理解爲一種功能性語言將帶來的一種優勢。

  This chapter will thoroughly examine JavaScript’s focus on functions, and give us a sound basis on which to bring our JavaScript code to a level that any master would be proud of.
  本章將深入研究JavaScript中的函數,爲我們提供一個可靠的基礎,使我們的javascript代碼達到任何一個大師都會感到自豪的水平。

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