Skip to content

Commit 3d0af4e

Browse files
committed
Add source files.
1 parent 0a98ccb commit 3d0af4e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+6382
-0
lines changed

.gitignore

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
Thumbs.db
2+
ehthumbs.db
3+
[Dd]esktop.ini
4+
$RECYCLE.BIN/
5+
.DS_Store
6+
.klive
7+
.dropbox.cache
8+
9+
*.tmp
10+
*.bak
11+
*.swp
12+
*.lnk
13+
14+
.svn
15+
.idea
16+
17+
node_modules/
18+
bower_components/
19+
npm-debug.log
20+
21+
*.zip
22+
*.gz
23+
24+
_raw/
25+
reader.txt

chapter-0/000-preface.md

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
# [] [PJA] [000] 序言
2+
3+
# Preface
4+
5+
## Conventions Used in This Book
6+
7+
The following typographical conventions are used in this book:
8+
9+
_Italic_
10+
11+
Indicates new terms, URLs, email addresses, filenames, and file extensions.
12+
13+
`Constant width`
14+
15+
Used for program listings, as well as within paragraphs to refer to program elements such as variable or function names, databases, data types, environment variables, statements, and keywords.
16+
17+
**`Constant width bold`**
18+
19+
Shows commands or other text that should be typed literally by the user.
20+
21+
_`Constant width italic`_
22+
23+
Shows text that should be replaced with user-supplied values or by values determined by context.
24+
25+
> `[$]` This icon signifies a tip, suggestion, or general note.
26+
>
27+
> `[!]` This icon indicates a warning or caution.
28+
29+
## Using Code Examples
30+
31+
This book is here to help you get your job done. In general, you may use the code in this book in your programs and documentation. You do not need to contact us for permission unless you’re reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from this book does not require permission. Selling or distributing a CD-ROM of examples from O'Reilly books does require permission. Answering a question by citing this book and quoting example code does not require permission. Incorporating a significant amount of example code from this book into your product’s documentation does require permission.
32+
33+
We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN. For example: “_Programming JavaScript Applications_ by Eric Elliott (O'Reilly). Copyright 2013 Eric Elliott, 978-1-4493-2094-2.”
34+
35+
If you feel your use of code examples falls outside fair use or the permission given above, feel free to contact us at <permissions#oreilly.com>.
36+
37+
## Safari&reg; Books Online
38+
39+
> Safari Books Online is an on-demand digital library that lets you easily search over 7,500 technology and creative reference books and videos to find the answers you need quickly.
40+
41+
With a subscription, you can read any page and watch any video from our library online. Read books on your cell phone and mobile devices. Access new titles before they are available for print, and get exclusive access to manuscripts in development and post feedback for the authors. Copy and paste code samples, organize your favorites, download chapters, bookmark key sections, create notes, print out pages, and benefit from tons of other time-saving features.
42+
43+
O'Reilly Media has uploaded this book to the Safari Books Online service. To have full digital access to this book and others on similar topics from O'Reilly and other publishers, sign up for free at [http://my.safaribooksonline.com/][6].
44+
45+
## How to Contact Us
46+
47+
Please address comments and questions concerning this book to the publisher:
48+
49+
> O'Reilly Media, Inc.
50+
> 1005 Gravenstein Highway North
51+
> Sebastopol, CA 95472
52+
> 800-998-9938 (in the United States or Canada)
53+
> 707-829-0515 (international or local)
54+
> 707-829-0104 (fax)
55+
56+
We have a web page for this book, where we list errata, examples, and any additional information. You can access this page at:
57+
58+
<http://shop.oreilly.com/product/0636920024231.do>
59+
60+
To comment or ask technical questions about this book, send email to:
61+
62+
<bookquestions#oreilly.com>
63+
64+
For more information about our books, courses, conferences, and news, see our website at <http://www.oreilly.com/>.
65+
66+
Find us on Facebook: <http://facebook.com/oreilly>
67+
68+
Follow us on Twitter: <http://twitter.com/oreillymedia>
69+
70+
Watch us on YouTube: <http://www.youtube.com/oreillymedia>
71+
72+
## Introduction
73+
74+
There are many books on the web technologies covered in this book. However, there are precious few JavaScript books that can be recommended to somebody who wants to learn how to build a complete JavaScript application from the ground up. Meanwhile, almost every new tech startup needs knowledgeable JavaScript application developers on staff. This book exists for one purpose: To help you gain the knowledge you need to build complete JavaScript applications that are easy to extend and maintain.
75+
76+
This book is not intended to teach you the basics of JavaScript. Instead, it’s designed to build on your existing knowledge and discuss JavaScript features and techniques that will make your code easier to work with over time. Normally, as an application grows, it becomes increasingly difficult to add new features and fix bugs. Your code becomes too rigid and fragile, and even a small change could necessitate a lengthy refactor. If you follow the patterns outlined in this book, your code will remain flexible and resilient. Changes to one piece of code won't negatively impact another.
77+
78+
The book will focus primarily on client-side architecture, although it will cover server-side topics such as basic RESTful APIs and Node. The trend is that a great deal of the application logic is getting pushed to the client. It was once the case that the server environment would handle things like templating and communication with vendor services. Now it’s common to deal with both of those jobs inside the browser.
79+
80+
In fact, a modern JavaScript application does almost everything a traditional desktop app would do completely in the browser. Of course, servers are still handy. Server roles frequently include serving static content and dynamically loaded modules, data persistence, action logging, and interfacing with third party APIs.
81+
82+
We'll cover:
83+
84+
* JavaScript features and best practices for application developers
85+
86+
* Code organization, modularity, and reuse
87+
88+
* Separation of concerns on the client side (MVC, etc...)
89+
90+
* Communicating with servers and APIs
91+
92+
* Designing and programming RESTful APIs with Node.js
93+
94+
* Build, test, collaboration, deployment, and scaling
95+
96+
* Expanding reach via platform targets and internationalization
97+
98+
## Who this Book is For
99+
100+
You have some experience with JavaScript; at least a year or two working frequently with the language, but you want to learn more about how you can apply it specifically to developing robust web scale or enterprise applications.
101+
102+
You know a thing or two about programming, but you have an insatiable thirst to learn more. In particular, you'd like to learn more about how to apply the powerful features that distinguish JavaScript from other languages, such as closures, functional programming, and prototypal inheritance (even if this is the first you've heard of them).
103+
104+
Perhaps you'd also like to learn about how to apply Test Driven Development (TDD) techniques to your next JavaScript challenge. This book uses tests throughout the code examples. By the time you reach the end, you should be in the habit of thinking about how you'll test the code you write.
105+
106+
## Who this Book is Not For
107+
108+
This book covers a lot of ground quickly. It was not written with the beginner in mind, but if you need clarification, you might find it in "JavaScript: The Good Parts" by Douglas Crockford, "JavaScript: The Definitive Guide", by David Flannagan, or for help with software design patterns, the famous Gang of Four book (GoF), "Design Patterns: Elements of Reusable Object-Oriented Software" by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides.
109+
110+
Google and Wikipedia can be handy guides to help you through as well. Wikipedia is a fairly good reference for software design patterns.
111+
112+
If this is your first exposure to JavaScript, you would do well to study some introductory texts and tutorials before you attempt to tackle this book. My favorite is ["Eloquent JavaScript" by Marijn Haverbeke][8]. Be sure to follow that up with "JavaScript: The Good Parts" mentioned above, and pay special attention to the style guide in the beginning of this book so that you can learn from the mistakes of more experienced JavaScript developers.
113+
114+
## About the Author
115+
116+
Eric Elliott is a veteran of JavaScript application development. He is currently a member of the Creative Cloud team at Adobe. Previous roles include JavaScript Lead at Tout (social video), Senior JavaScript Rockstar at BandPage (an industry leading music app), head of client side architecture at Zumba Fitness (the leading global fitness brand), and several years as a UX and viral application consultant. He lives in the San Francisco bay area with the most beautiful woman in the world.
117+
118+
[6]: http://my.safaribooksonline.com/?portal=oreilly
119+
[8]: http://eloquentjavascript.net/
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# [] [PJA] [100] 第一章 JavaScript 革命
2+
3+
# Chapter 1. The JavaScript Revolution
4+
5+
# 第一章 JavaScript 革命
6+
7+
In case you haven’t heard, JavaScript is arguably the most important programming language on Earth. Once thought of as a toy, JavaScript is now the most widely deployed programming language in history. Almost everyone with a computer or a smart phone has all the tools they need to execute JavaScript programs, and create their own. All you need is a browser and a text editor.
8+
9+
你可能还不知道,JavaScript 可以说是地球上最重要的程序语言。虽然曾经被看作“玩具语言”,但 JavaScript 目前已经成为史上应用最广的程序语言。拥有一台电脑或智能手机,就相当于拥有了执行(甚至创建)JavaScript 程序的必备工具。你所需要的一切,就是一款网页浏览器,加上文本编辑器。
10+
11+
JavaScript, HTML and CSS have become so prevalent that many operating systems have adopted the open web standards as the presentation layer for native apps. Even Microsoft announced that the Windows 8 UI will be driven by open web standards.
12+
13+
JavaScript、HTML 和 CSS 已经变得如此流行,以至于许多操作系统已经采用了这些开放的 Web 标准作为原生应用的表现层。甚至微软公司也声称 Windows 8 的 UI 层将由这些开放的 Web 标准技术来驱动。
14+
15+
Creating a JavaScript program is as simple as editing a text file and opening it in the browser. No complex development environments to download and install. No complex IDE to learn. JavaScript is easy to learn, too. The basic syntax is immediately familiar to any programmer who has been exposed to the C family syntax. No other language can boast a barrier to entry as low as JavaScript.
16+
17+
创建一个 JavaScript 程序十分简单,编辑一个文本文件并在浏览器中打开它就行了。并不需要下载安装复杂的开发环境;不需要学习复杂的 IDE 工具;JavaScript 语言本身也十分易学。对于任何一个接触过类 C 语法的程序员来说,JavaScript 的基本语法可以立即上手。没有其它语言可以像 JavaScript 这样拥有如此之低的学习门槛。
18+
19+
That low barrier to entry is probably the main reason that JavaScript was once widely (perhaps rightly) shunned as a toy. It was mainly used to create UI effects in the browser. That situation has changed.
20+
21+
“低门槛”可能就是当年 JavaScript 被普遍地贬低为一门玩具语言的主要原因(或许人们并没说错)。那时它主要用于在浏览器中创建 UI 特效。但是,时局已变。
22+
23+
For a long time, there was no way to save data with JavaScript. If you wanted data to persist, you had to submit a form to a web server and wait for a page refresh. That hindered the process of creating responsive and dynamic web applications. However, in 2000, Microsoft started shipping Ajax technology in Internet Explorer. Soon after, other browsers added support for the XMLHttpRequest object.
24+
25+
在相当长的时间内,JavaScript 无法保存数据。如果你想把数据持久保存,就只能通过表单把数据提交到一台 Web 服务器,并等待页面刷新。这阻碍了创建快速响应的、动态的 Web 应用的步伐。然而在 2000 年,微软开始在 IE 浏览器中搭载 Ajax 技术。此后不久,其它浏览器也纷纷增加了对 XMLHttpRequest 对象的支持。
26+
27+
In 2004, Google launched Gmail. Initially applauded because it promised users nearly infinite storage for their email, Gmail also brought a major revolution. Gone were the page refreshes. Under the hood, Gmail was taking advantage of the new Ajax technology, creating a single page, fast, responsive web application that would forever change the way that web applications are designed.
28+
29+
2004 年,Google 发布 Gmail。这项服务首先通过近乎无限的存储空间赢得了用户的喝彩,同时它也引发了一场重大变革。“页面刷新”一去不复返了。在程序内部,Gmail 得益于全新的 Ajax 技术,打造了一款单页的、快速响应的 Web 应用程序,它永久改变了 Web 应用程序的设计方式。
30+
31+
Since that time, web developers have produced nearly every type of application, including full blown cloud-based office suites (see [Zoho.com][5]), social APIs like Facebook’s JavaScript SDK, even graphically intensive video games.
32+
33+
从那时开始,Web 开发者们实现了各种类型的应用程序,这其中包括完全成熟的基于云端的办公套件(参见 [Zoho.com][5])、以 Facebook 的 JavaScript SDK 为代表的社会化 API、甚至是图形密集的电子游戏等等。
34+
35+
JavaScript didn’t just luck into its position as the dominant client side language on the web. It is actually very well suited to be the language that took over the world. It is one of the most advanced and expressive programming languages developed to date. Here are some of the features you may or may not be familiar with:
36+
37+
JavaScript 并没有固步于 Web 客户端的垄断地位,它实际上也十分适合成为那种掌管一切的语言。它是人类迄今为止创造出的最先进、最具表现力的程序语言之一。以下是一些你可能熟悉(或不熟悉)的关于它的特征。
38+
39+
[5]: http://zoho.com/

chapter-1/101-performance.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# [] [PJA] [101] 性能
2+
3+
## Performance
4+
5+
## 性能
6+
7+
Just in Time compiling -- In modern browsers, most JavaScript is compiled, highly optimized and executed like native code, so run-time performance is close to software written in C or C\+\+. Of course, there is still the overhead of garbage collection and dynamic binding, so it is possible to do certain things faster -- however, the difference is generally not worth sweating over until you’ve optimized everything else. With [Node.js][6] (a high-performance, evented, server-side JavaScript environment built on Google's highly optimized V8 JavaScript engine), JavaScript apps are event-driven and non-blocking, which generally more than makes up the code execution difference between JavaScript and less dynamic languages.
8+
9+
JIT 编译——在现代浏览器中,大多数 JavaScript 代码会被编译和高度优化、并以接近原生代码的方式执行,所以运行时性能非常接近以 C 或 C++ 编写的软件。当然,仍然会存在由垃圾回收和动态绑定所产生的额外消耗,所以在某些场景下仍有提速的空间——不过这种差异并不值得纠结,除非你已经把其它所有事情都优化到位了。随着 [Node.js][6](一个基于 Google 高度优化的 V8 引擎的、高性能的、事件化的、服务器端的 JavaScript 环境)的到来,JavaScript 应用开始采用事件驱动、无阻塞的方式。相对于试图弥补 JavaScript 和静态语言之间的代码执行差异,这个思路总体上要强得多。
10+
11+
[6]: http://nodejs.com/

chapter-1/102-objects.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# [] [PJA] [102] 对象
2+
3+
## Objects
4+
5+
JavaScript has very rich Object Oriented (OO) features. The _JSON_ (JavaScript Object Notation) standard used in nearly all modern web applications for both communication and data persistence is a subset of JavaScript’s excellent object literal notation.
6+
7+
JavaScript uses a _prototypal inheritance_ model. Instead of classes, you have object prototypes. New objects automatically inherit methods and attributes of its parent object through the _prototype chain_. It’s possible to modify an object’s prototype at any time, making JavaScript a very flexible, dynamic language.
8+
9+
Evidence: It’s possible to mimic Java’s class-based OO and inheritance models in JavaScript virtually feature-for-feature, and in most cases, with less code. The reverse is not true.
10+
11+
Contrary to common belief, JavaScript supports features like encapsulation, polymorphism, multiple inheritance, composition, and much more.

chapter-1/103-syntax.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# [] [PJA] [103] 语法
2+
3+
## Syntax
4+
5+
The JavaScript syntax should be immediately familiar to anybody who has experience with C-family languages such as C\+\+, Java, C#, PHP, and so on. Part of JavaScript's popularity is due to its familiarity, though it's important to understand that JavaScript behaves very differently from all of these under the hood.
6+
7+
JavaScript’s Object Literal syntax is so simple, flexible, and concise, it was adapted to become the dominant standard for client/server communication in the form of JSON, which is more compact and flexible than the XML that it replaced.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# [] [PJA] [104] 一等公民——函数
2+
3+
## First Class Functions
4+
5+
In JavaScript, objects were not a tacked-on afterthought. Nearly everything in JavaScript is an object, including functions. Because of that feature, functions can be used anywhere you might use a variable, including the parameters in function calls. That feature is often used to define anonymous callback functions for asynchronous operations, or to create _higher order functions_ (functions that take other functions as parameters, return a function, or both). Higher order functions are used in the _functional programming_ style to abstract away commonly repeated coding patterns such as iteration loops, or other instruction sets that differ mostly in the variables or data they consume.
6+
7+
Good examples of functional programing include functions like `.map()`, `.reduce()`, and `.forEach()`. The [Underscore.js][7] library contains many useful functional utilities. For simplicity, we'll be making use of Underscore.js in this book.
8+
9+
[7]: http://documentcloud.github.com/underscore/

chapter-1/105-events.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# [] [PJA] [105] 事件
2+
3+
## Events
4+
5+
Inside the browser, everything runs in an event loop. JavaScript coders quickly learn to think in terms of event handlers, and as a result, code from experienced JavaScript developers tends to be well organized and efficient. Operations that might block processing in other languages happen concurrently in JavaScript.
6+
7+
If you click something, you want something to happen instantly. That impatience has led to wonderful advancements in UI design, such as Google Instant, and the ground breaking address lookup on [The Wilderness Downtown][8]. (“The Wilderness Downtown” is an interactive short film by Chris Milk set to the Arcade Fire song, “We Used To Wait”. It was built entirely with the latest open web technologies.) Such functionality is powered by Ajax calls that do their thing in the background without slowing down the UI.
8+
9+
[8]: http://thewildernessdowntown.com/

chapter-1/106-reusability.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# [] [PJA] [106] 重用
2+
3+
## Reusability
4+
5+
JavaScript code, by virtue of its ubiquity, is the most portable, reusable code around. What other language lets you write the same code that runs natively on both the client and the server? (See the section on Node to learn about an event-driven JavaScript environment that is revolutionizing server-side development.)
6+
7+
JavaScript can be modular and encapsulated, and it is common to see scripts written by six different teams of developers who have never communicated working in harmony on the same page.

0 commit comments

Comments
 (0)