Skip to content

Commit

Permalink
An Introduction to JavaScript (#5)
Browse files Browse the repository at this point in the history
* An Introduction to JavaScript

* misc modifications
  • Loading branch information
lenchen1112 committed Aug 21, 2019
1 parent 3c0d40b commit fcf8013
Showing 1 changed file with 68 additions and 67 deletions.
135 changes: 68 additions & 67 deletions 1-js/01-getting-started/1-intro/article.md
Original file line number Diff line number Diff line change
@@ -1,120 +1,121 @@
# An Introduction to JavaScript
# 介紹 JavaScript

Let's see what's so special about JavaScript, what we can achieve with it, and which other technologies play well with it.
一起來看看 JavaScript 有什麼特別的,我們能做些什麼,與哪些配合不錯的技術。

## What is JavaScript?
## 什麼是 JavaScript?

*JavaScript* was initially created to *"make web pages alive"*.
*JavaScript* 最初是為了 *"賦予網頁活力"*

The programs in this language are called *scripts*. They can be written right in a web page's HTML and run automatically as the page loads.
這種程式語言我們稱之為 *腳本*,它們可以被寫入網頁 HTML 中,並在頁面讀取時自動執行。

Scripts are provided and executed as plain text. They don't need special preparation or compilation to run.
腳本以純文字檔的型式被提供並執行,它們不需要特別的前置作業或編譯即可運作。

In this aspect, JavaScript is very different from another language called [Java](https://en.wikipedia.org/wiki/Java_(programming_language)).
在這方面,JavaScript 和另一個稱為 [Java](https://en.wikipedia.org/wiki/Java_(programming_language)) 的程式語言有很大的不同。

```smart header="Why <u>Java</u>Script?"
When JavaScript was created, it initially had another name: "LiveScript". But Java was very popular at that time, so it was decided that positioning a new language as a "younger brother" of Java would help.
```smart header="為什麼叫 <u>Java</u>Script?"
JavaScript 剛誕生時原先有另一個名字: "LiveScript"。但 Java 在那時非常流行,因此被定位成一個 Java 的 "小弟" 將有助於推廣。
But as it evolved, JavaScript became a fully independent language with its own specification called [ECMAScript](http://en.wikipedia.org/wiki/ECMAScript), and now it has no relation to Java at all.
但隨著它發展,JavaScript 變成一個完全獨立的語言並有其規範 [ECMAScript](http://en.wikipedia.org/wiki/ECMAScript) ,現在它與 Java 之間沒有任何關係。
```

Today, JavaScript can execute not only in the browser, but also on the server, or actually on any device that has a special program called [the JavaScript engine](https://en.wikipedia.org/wiki/JavaScript_engine).
現在,JavaScript 不僅可執行於瀏覽器中,還可運作於伺服器上,或任何具有 [JavaScript 引擎](https://en.wikipedia.org/wiki/JavaScript_engine) 的裝置內。

The browser has an embedded engine sometimes called a "JavaScript virtual machine".
瀏覽器內嵌有 JavaScript 引擎,有時也被稱為 "JavaScript 虛擬機" (JavaScript virtual machine)。

Different engines have different "codenames". For example:
不同的引擎有不同的 "代號 (codenames)"。例如:

- [V8](https://en.wikipedia.org/wiki/V8_(JavaScript_engine)) -- in Chrome and Opera.
- [SpiderMonkey](https://en.wikipedia.org/wiki/SpiderMonkey) -- in Firefox.
- ...There are other codenames like "Trident" and "Chakra" for different versions of IE, "ChakraCore" for Microsoft Edge, "Nitro" and "SquirrelFish" for Safari, etc.
- [V8](https://en.wikipedia.org/wiki/V8_(JavaScript_engine)) -- Chrome Opera 內的引擎。
- [SpiderMonkey](https://en.wikipedia.org/wiki/SpiderMonkey) -- Firefox 內的引擎。
- ... 還有其他代號,像是不同版本 IE 使用的 "Trident" "Chakra"、微軟 Edge 的 "ChakraCore"、Safari 的 "Nitro" "SquirrelFish",等等。

The terms above are good to remember because they are used in developer articles on the internet. We'll use them too. For instance, if "a feature X is supported by V8", then it probably works in Chrome and Opera.
上面提到的代號最好可以記住,因為這些代號常被用於網路上的開發者文章,就像我們一樣。如:"V8 支援某個 X 功能",代表該功能在 Chrome Opera 上應該可以運作。

```smart header="How do engines work?"
```smart header="引擎怎麼運作的?"
Engines are complicated. But the basics are easy.
引擎很複雜,但概念很簡單。
1. The engine (embedded if it's a browser) reads ("parses") the script.
2. Then it converts ("compiles") the script to the machine language.
3. And then the machine code runs, pretty fast.
1. 引擎 (瀏覽器內建) 讀取 ("解析") 腳本
2. 接著轉換 ("編譯") 腳本為機器語言
3. 然後機器語言極快地執行
The engine applies optimizations at each step of the process. It even watches the compiled script as it runs, analyzes the data that flows through it, and applies optimizations to the machine code based on that knowledge. When it's done, scripts run quite fast.
引擎會對流程中每個階段進行優化。甚至會在執行時監看編譯好的腳本,分析其資料流並以此再優化為機器碼,由此腳本可以快速地執行。
```

## What can in-browser JavaScript do?
## 瀏覽器中的 JavaScript 可以做什麼?

Modern JavaScript is a "safe" programming language. It does not provide low-level access to memory or CPU, because it was initially created for browsers which do not require it.
現代化 JavaScript 是個 "安全" 的程式語言。它不提供對記憶體或 CPU 的低階存取,因為它原生是為了瀏覽器而建立,所以不需要。

JavaScript's capabilities greatly depend on the environment it's running in. For instance, [Node.js](https://wikipedia.org/wiki/Node.js) supports functions that allow JavaScript to read/write arbitrary files, perform network requests, etc.
JavaScript 的能力很大一部分取決於執行它的環境。例如 [Node.js](https://wikipedia.org/wiki/Node.js) 提供 JavaScript 可以讀寫任意檔案與發送網路請求 (network requests) 等功能。

In-browser JavaScript can do everything related to webpage manipulation, interaction with the user, and the webserver.
瀏覽器中的 JavaScript 可以做與網頁操作、使用者互動和網頁伺服器相關的任何事情。

For instance, in-browser JavaScript is able to:
例如,瀏覽器中的 JavaScript 能夠:

- Add new HTML to the page, change the existing content, modify styles.
- React to user actions, run on mouse clicks, pointer movements, key presses.
- Send requests over the network to remote servers, download and upload files (so-called [AJAX](https://en.wikipedia.org/wiki/Ajax_(programming)) and [COMET](https://en.wikipedia.org/wiki/Comet_(programming)) technologies).
- Get and set cookies, ask questions to the visitor, show messages.
- Remember the data on the client-side ("local storage").
- 在頁面加入新的 HTML ,改變既有內容與樣式
- 與使用者互動,執行滑鼠點擊、游標移動和按鍵按壓
- 經由網路發送請求給遠端伺服器,下載並上傳檔案 (亦稱為 [AJAX](https://en.wikipedia.org/wiki/Ajax_(programming)) [COMET](https://en.wikipedia.org/wiki/Comet_(programming)) 技術)
- 取得與設定 cookies、詢問訪客和顯示訊息
- 記得客戶端 (client-side) 的資料 ("local storage")

## What CAN'T in-browser JavaScript do?
## 瀏覽器中的 JavaScript **不能**做什麼?

JavaScript's abilities in the browser are limited for the sake of the user's safety. The aim is to prevent an evil webpage from accessing private information or harming the user's data.
為了使用者的資訊安全,JavaScript 在瀏覽器內的功能被限制。此為防範惡意網頁獲取私人資訊或損害使用者資料。

Examples of such restrictions include:
這些限制範例如下:

- JavaScript on a webpage may not read/write arbitrary files on the hard disk, copy them or execute programs. It has no direct access to OS system functions.
- 網頁上的 JavaScript 無法讀寫、複製和執行硬碟內任意檔案。它也沒有直接存取作業系統的功能。

Modern browsers allow it to work with files, but the access is limited and only provided if the user does certain actions, like "dropping" a file into a browser window or selecting it via an `<input>` tag.
現代化瀏覽器允許 JavaScript 有限制地操作檔案,且只有在使用者做特定動作時提供,像是:"拖曳" 一個檔案至瀏覽器視窗,或經由 `<input>` 標籤選取。

There are ways to interact with camera/microphone and other devices, but they require a user's explicit permission. So a JavaScript-enabled page may not sneakily enable a web-camera, observe the surroundings and send the information to the [NSA](https://en.wikipedia.org/wiki/National_Security_Agency).
- Different tabs/windows generally do not know about each other. Sometimes they do, for example when one window uses JavaScript to open the other one. But even in this case, JavaScript from one page may not access the other if they come from different sites (from a different domain, protocol or port).
JavaScript 有些方法可與相機、麥克風或其他裝置互動,但都需要使用者明確地授權。所以啟用 JavaScript 的網頁不會偷偷開啟相機觀察周遭並傳資料給 [美國國家安全局 (NSA)](https://en.wikipedia.org/wiki/National_Security_Agency)
- 不同的瀏覽器分頁/視窗基本上不知道彼此,但有時例外,例如:當一個視窗使用 JavaScript 開啟另一個視窗時。但就算如此,開啟不同網站 (不同域名、通訊協定或埠) 的頁面,其中的 JavaScript 亦無法溝通。

This is called the "Same Origin Policy". To work around that, *both pages* must agree for data exchange and contain a special JavaScript code that handles it. We'll cover that in the tutorial.
這被稱為 "同源政策" (Same Origin Policy)。為了解決它,*兩個頁面* 都必須同意資料交換和包含一隻特殊的 JavaScript 程式來處理這件事。我們的教程將會包含這部分。

This limitation is, again, for the user's safety. A page from `http://anysite.com` which a user has opened must not be able to access another browser tab with the URL `http://gmail.com` and steal information from there.
- JavaScript can easily communicate over the net to the server where the current page came from. But its ability to receive data from other sites/domains is crippled. Though possible, it requires explicit agreement (expressed in HTTP headers) from the remote side. Once again, that's a safety limitation.
再次強調,這個限制是為了使用者的資安考量。一個使用者從 `http://anysite.com` 打開的頁面不該能存取另一個瀏覽器分頁 `http://gmail.com` 內的資訊。
- JavaScript 可以簡單地經由網路與目前頁面來源的伺服器溝通。但它從別的網站/域名接收資料的能力受到限制。儘管可以也需要遠端明確地授權 (在 HTTP headers 中)。再一次強調,這是為了資安而限制。

![](limitations.svg)

Such limits do not exist if JavaScript is used outside of the browser, for example on a server. Modern browsers also allow plugin/extensions which may ask for extended permissions.
在瀏覽器以外的 JavaScript 就沒有這些限制,如:伺服器上的 JavaScript。現代化瀏覽器也允許插件/擴充套件要求額外權限。

## What makes JavaScript unique?
## 是什麼讓 JavaScript 如此獨特?

There are at least *three* great things about JavaScript:
至少有 *三項* JavaScript 很棒的事:

```compare
+ Full integration with HTML/CSS.
+ Simple things are done simply.
+ Support by all major browsers and enabled by default.
+ HTML/CSS 完整整合
+ 簡單的事能夠簡單地完成
+ 所有主要瀏覽器支援且預設開啟
```
JavaScript is the only browser technology that combines these three things.
在瀏覽器技術中,只有 JavaScript 能唯一滿足這三項。

That's what makes JavaScript unique. That's why it's the most widespread tool for creating browser interfaces.
這造就 JavaScript 如此獨特。這也是為什麼它是建立瀏覽器介面最為廣泛的工具。

That said, JavaScript also allows to create servers, mobile applications, etc.
此外,JavaScript 也可以建立伺服器和手機應用程式等等。

## Languages "over" JavaScript
## JavaScript "之上" 的語言

The syntax of JavaScript does not suit everyone's needs. Different people want different features.
JavaScript 的語法並不符合每個人的要求,不同人想要不同功能。

That's to be expected, because projects and requirements are different for everyone.
這是預期中的,因為每個人的計畫和需求都不一樣。

So recently a plethora of new languages appeared, which are *transpiled* (converted) to JavaScript before they run in the browser.
所以最近有大量新語言出現,它們在被瀏覽器執行前,都被 *轉譯* (transpiled) 成 JavaScript

Modern tools make the transpilation very fast and transparent, actually allowing developers to code in another language and auto-converting it "under the hood".
現代化的工具讓轉譯迅速且透明,且實際上使開發者用另一種語言寫程式,再被自行轉換成 JavaScript。

Examples of such languages:
例如這些語言:

- [CoffeeScript](http://coffeescript.org/) is a "syntactic sugar" for JavaScript. It introduces shorter syntax, allowing us to write clearer and more precise code. Usually, Ruby devs like it.
- [TypeScript](http://www.typescriptlang.org/) is concentrated on adding "strict data typing" to simplify the development and support of complex systems. It is developed by Microsoft.
- [Flow](http://flow.org/) also adds data typing, but in a different way. Developed by Facebook.
- [Dart](https://www.dartlang.org/) is a standalone language that has its own engine that runs in non-browser environments (like mobile apps), but also can be transpiled to JavaScript. Developed by Google.
- [CoffeeScript](http://coffeescript.org/) 是組 JavaScript 的"語法糖"。它有更簡短的語法,可以讓我們寫出更清楚且精確的程式,通常 Ruby 開發者愛用。
- [TypeScript](http://www.typescriptlang.org/) 致力於增加 "強型態確認 (strict data typing)" 來簡化開發與支援複雜的系統,為微軟所開發。
- [Flow](http://flow.org/) 同樣增加型態確認但使用不同方式,為臉書所開發。
- [Dart](https://www.dartlang.org/) 是一種擁有自己引擎的獨立語言,它執行在非瀏覽器環境上 (像是手機應用程式),但也可以轉譯成 JavaScript,為谷歌所開發。

There are more. Of course, even if we use one of transpiled languages, we should also know JavaScript to really understand what we're doing.
還有更多其他語言。當然,就算我們使用某種轉譯式語言,我們應該也要了解 JavaScript 來真正知道我們在做什麼。

## Summary
## 總結

- JavaScript 最初被建為只用在瀏覽器上的語言,但現在也被其他多種環境所使用。
- 至今 JavaScript 有著獨特的地位,它是在瀏覽器上最被廣泛採用的語言且與 HTML/CSS 完整整合。
- 有許多語言可被 "轉譯" 成 JavaScript 並提供特定的功能。建議在掌握 JavaScript 後可以稍微看看。

- JavaScript was initially created as a browser-only language, but is now used in many other environments as well.
- Today, JavaScript has a unique position as the most widely-adopted browser language with full integration with HTML/CSS.
- There are many languages that get "transpiled" to JavaScript and provide certain features. It is recommended to take a look at them, at least briefly, after mastering JavaScript.

0 comments on commit fcf8013

Please sign in to comment.