翻译 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代码达到任何一个大师都会感到自豪的水平。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值