翻譯:《JavaScript 權威指南(第5版)》第一章(一)

來源:互聯網
上載者:User
文章目錄
  • 1.1. What Is JavaScript?
  • 1.1. 什麼是 JavaScript
  • 1.1.1. JavaScript Is Not Java
  • 1.1.1. JavaScript 並非 Java
  • 1.1.2. JavaScript Is Not Simple
  • 1.1.2. JavaScript 並不簡單
  • 1.2. Versions of JavaScript
  • 1.2. JavaScript 版本
  • 1.3. Client-Side JavaScript
  • 用戶端 JavaScript
  • 1.3.1. Client-Side JavaScript Examples
  • 1.3.1. 用戶端 JavaScript 舉例

聲明:翻譯只有一個目的:學慣用途。若有著作權問題請及時聯絡本人。

本貼文根據篇幅將第一章的翻譯分為兩個部分,這是第一部分的內容。

Chapter 1. Introduction to JavaScript第一章 Javascript 概論

JavaScript is an interpreted programming language with object-oriented (OO) capabilities. Syntactically, the core JavaScript language resembles C, C++, and Java, with programming constructs such as the if statement, the while loop, and the && operator. The similarity ends with this syntactic resemblance, however. JavaScript is a loosely typed language, which means that variables do not need to have a type specified. Objects in JavaScript map property names to arbitrary property values. In this way, they are more like hash tables or associative arrays (in Perl) than they are like structs (in C) or objects (in C++ or Java). The OO inheritance mechanism of JavaScript is prototype-based like that of the little-known language Self. This is quite different from inheritance in C++ and Java. Like Perl, JavaScript is an interpreted language, and it draws inspiration from Perl in a number of areas, such as its regular-expression and array-handling features.

JavaScript 是一門具有物件導向(OO)能力的解釋型程式設計語言。在文法上,JavaScript語言核心與 C、C++ 及 Java 相似,雖然具有諸如 if 語句、while 迴圈、&& 運算子這樣的程式結構,但是也僅限於在文法上與之相似。JavaScript 是一門弱類型的語言,也就是說,變數的類型不必指明。JavaScript 中的對象可映射屬性名稱到隨意的屬性值。在這點上,它更像散列表或關聯陣列(在 Perl 中),而不像結構(在 C 中)或對象(在 C++ 或 Java 中)。JavaScript 中物件導向的繼承機制是基於原型的,這與 Self 語言有點相似。卻與 C++ 和 Java 的繼承則大不相同。Java是門解釋型語言(與 Perl 一樣),而且多處靈感是受 Perl 語言的啟發,諸如Regex和數組處理的特性。

The core JavaScript language supports numbers, strings, and Boolean values as primitive datatypes. It also includes built-in support for array, date, and regular-expression objects.

JavaScript 語言核心支援數字、字串、布爾值這些未經處理資料類型,也包括對數組、日期、Regex這些對象的內建支援。

JavaScript is most commonly used in web browsers, and, in that context, the general-purpose core is extended with objects that allow scripts to interact with the user, control the web browser, and alter the document content that appears within the web browser window. This embedded version of JavaScript runs scripts embedded within HTML web pages. It is commonly called client-side JavaScript to emphasize that scripts are run by the client computer rather than the web server.

JavaScript 用得最多的一般是在 網頁瀏覽器中,在其上下文中,通用渲染核心使允許使用者利用指令碼來擴充控制瀏覽器中的對象,更改文檔內容的 網頁瀏覽器視窗的外觀。JavaScript 的嵌入版運行 HTML 網頁嵌入指令碼。一般稱為用戶端 JavaScript來表示指令碼是運行在客戶電腦上而不是 網頁伺服器。

The core JavaScript language and its built-in datatypes are the subject of international standards, and compatibility across implementations is very good. Parts of client-side JavaScript are formally standardized, other parts are de facto standards, and other parts are browser-specific extensions. Cross-browser compatibility is often an important concern for client-side JavaScript programmers.

JavaScript 語言核心及其內建資料類型是國際標準化的項目,對相容實現跨平台是很有意義的。用戶端 JavaScript 的部分已被正式標準化了,而特定瀏覽器延伸的部分已是實際上的標準。跨瀏覽器安全色是用戶端 JavaScript 程式員們常關注的重點之一。

This chapter is a high-level overview of JavaScript; it provides the background information you need before embarking on a study of the language. As motivation and introduction, it includes some simple examples of client-side JavaScript code.

本章是對 JavaScript 的進階概覽。它提供了語言學習上手前的背景資訊。還有目標和導論,包括一些用戶端 JavaScript 範例程式碼。

1.1. What Is JavaScript?1.1. 什麼是 JavaScript

JavaScript is the subject of a fair bit of misinformation and confusion. Before proceeding any further, it is important to debunk two common and persistent myths about the language.

JavaScript 是相當容易誤解和混淆的主題。在對它進行進一步研究之前,有必要澄清兩個長期存在的對於該語言的誤解。

1.1.1. JavaScript Is Not Java1.1.1. JavaScript 並非 Java

One of the most common misconceptions about JavaScript is that it is a simplified version of Java, the programming language from Sun Microsystems. Other than an incomplete syntactic resemblance and the fact that both Java and JavaScript can provide executable content in web browsers, the two languages are entirely unrelated. The similarity of names is purely a marketing ploy by Netscape and Sun (the language was originally called LiveScript; its name was changed to JavaScript at the last minute). However, JavaScript can, in fact, script Java (see Chapters 12 and 23).

對 JavaScript 最常見的誤解是認為它是Sun 微系統的 Java 程式設計語言的精簡版。但是除了文法上有些不完全相似和都提供了 網頁瀏覽器可執行內容之外,兩者則完全不相干。相似的名稱只是市場營銷的手段罷了(該語言最初被稱為 LiveScript,只是到最後那次被改稱 JavaScript)。但實際上有 script Java(請見第12章和第23章)。

1.1.2. JavaScript Is Not Simple1.1.2. JavaScript 並不簡單

Because JavaScript is interpreted instead of compiled, it is often considered a scripting language instead of a true programming language. The implication is that scripting languages are simpler and that they are programming languages for nonprogrammers. The fact that JavaScript is loosely typed does make it somewhat more forgiving for unsophisticated programmers. And many web designers have been able to use JavaScript for limited, cookbook-style programming tasks.

因為 JavaScript 是解釋型而非編譯型,所以一般被作為指令碼語言而非真正的程式設計語言來考慮。指令碼語言總被看成很簡單,是非程式員的程式設計語言。但事實上 JavaScript 的鬆散類型容許非科班出身的程式員編寫得靈活而不規範。許多 Web設計人員可用 JavaScript 來執行有限的、照葫蘆畫瓢的編程任務。

Beneath its thin veneer of simplicity, however, JavaScript is a full-featured programming language, as complex as any and more complex than some. Programmers who attempt to use JavaScript for nontrivial tasks often find the process frustrating if they do not have a solid understanding of the language. This book documents JavaScript comprehensively so that you can develop a sophisticated understanding. If you are used to cookbook-style JavaScript tutorials, you may be surprised at the depth and detail of the chapters ahead.

而 JavaScript 卻不是那麼簡單,它是門具有完全特性的程式設計語言,有多複雜就有多複雜。程式員如果對 JavaScript 語言的用法沒有紮實的理解,他在執行較複雜的任務時,就會發現進展困難重重。本書對JavaScript 無所不包,可使你能全面的理解。如果你是用菜譜式 JavaScript 教程,會對本章的深度和詳細感興趣。

1.2. Versions of JavaScript1.2. JavaScript 版本

Like any new technology, JavaScript evolved quickly when it was new. Previous editions of this book documented this evolution version by version, explaining exactly which language features were introduced in which version of the language. At the time of this writing, however, the language has stabilized and has been standardized by the European Computer Manufacturer's Association, or ECMA.[*] Implementations of this standard include the JavaScript 1.5 interpreter from Netscape and the Mozilla Foundation, and the JScript 5.5 interpreter from Microsoft. Any web browser newer than Netscape 4.5 or Internet Explorer 4 supports the latest version of the language. As a practical matter, you are unlikely to encounter a noncompliant interpreter.

如同任何新技術一樣,JavaScript 更新速度發展很快。本書的前幾版曆經了版本的迭代,該語言每個版本的特性都有詳細的介紹。在著作本書時,該語言趨於穩定,並且由歐洲電腦行業協會(或簡稱 ECMA)[注1]進行了標準化。該標準的實現方案包括了 Netscape 和 Mozilla 基金 的 JavaScript 1.5 解譯器、Microsoft 的 Jscript 5.5 解譯器。所有比 Netscape 4.5 或 IE 4 更新的 網頁瀏覽器都支援該語言的更新版本。實際上你也不願遇到非標準的解譯器。

[*] The standard is ECMA-262, version 3 (available at http://www.ecma-international.org/publications/files/ecma-st/ECMA-262.pdf).

注1:ECMA-262 v3 標準見 http://www.ecma-international.org/publications/files/ecma-st/ECMA-262.pdf

Note that the official name of the language, according to the ECMA-262 standard, is ECMAScript. But this awkward name is normally used only when making explicit reference to the standard. Technically, the name "JavaScript" refers only to language implementations from Netscape and the Mozilla Foundation. In practice, however, everyone calls the language JavaScript.

該語言的官方名稱按 ECMA-262 標準稱為 ECMAScript。但是這個蹩腳的名字一般只在想要詳查參考標準時才用。技術上,“JavaScript”只是指在 Netscape 和 Mozilla 基金的語言實現。而在實踐中,人們都稱該語言為 JavaScript。

After a long period of stability for JavaScript, there are now some signs of change. The Firefox 1.5 web browser from the Mozilla Foundation includes a new JavaScript interpreter with the version number 1.6. This version includes new (nonstandard) array manipulation methods described in Section 7.7.10, as well as support for E4X, which is described next.

JavaScript 版本穩定後現在又有了一些變動。Mozilla 基金的 Firefox 1.5 網頁瀏覽器包含 1.6版的 JavaScript 解譯器。此版包括了新的數組處理方法(見 7.7.10 章節)、並同時支援下一代的 E4X。

In addition to the ECMA-262 specification that standardizes the core JavaScript language, ECMA has released another JavaScript-related standard. ECMA-357 standardizes an extension to JavaScript known as E4X, or ECMAScript for XML. This extension adds an XML datatype to the language along with operators and statements for manipulating XML values. At the time of this writing, E4X is implemented only by JavaScript 1.6 and Firefox 1.5. E4X is not documented formally in this book, but Chapter 21 includes an extended introduction in tutorial form.

除了 JavaScript 語言核心標準化的 ECMA-262 規範外,還有另一個與 JavaScript 有關的 ECMA-357 標準。它將 E4X(ECMAScript for XML) 標準化。它將 XML 資料類型(datatype) 添加到語言裡結合動作陳述式和聲明語言來操作 XML 值。在著作本書時,E4X 只在 JavaScript 1.6 和 Firefox 1.5 中被實現。E4X 的內容在本書中的第21章有教程來介紹它。

Proposals for a fourth edition of the ECMA-262 specification, to standardize JavaScript 2.0, have been on the table for a number of years. These proposals describe a complete overhaul of the language, including strong typing and true class-based inheritance. To date, there has been little progress toward standardization of JavaScript 2.0. Nevertheless, implementations based on draft proposals include Microsoft's JScript.NET language and the ActionScript 2.0 and ActionScript 3.0 languages used in the Adobe (formerly Macromedia) Flash player. At the time of this writing, there are signs that work on JavaScript 2.0 is resuming, and the release of JavaScript 1.6 can be seen as a preliminary step in this direction. Any new version of the language is expected to be backward-compatible with the version documented here, of course. And even once JavaScript 2.0 is standardized, it will take a few years before it is universally deployed in web browsers.

ECMA 規範的第4版是討論了多年的 JavaScript 2.0 標準化的提議。這些提議描述了一個完整並改頭換面的語言,包含了強型別和真正基於類的繼承機制。迄今為止,JavaScript 2.0 的標準化已向前邁出了一小步。Microsoft 的 Jscript.NET 語言和用在 Adobe(以前的 Macromedia) Flash 播放器的 ActionScript 2.0 和 3.0 語言是基於草稿提議實現了。在著作本書時,JavaScript 2.0 還在繼續討論,JavaScript 1.6 的發布給了個方向。該語言的每次新版都必須向後相容,甚至是 JavaScript 2.0 標準,這幾年裡 JavaScript 1.6 會在 網頁瀏覽器中普遍部署。

1.3. Client-Side JavaScript用戶端 JavaScript

When a JavaScript interpreter is embedded in a web browser, the result is client-side JavaScript. This is by far the most common variant of JavaScript; when most people refer to JavaScript, they usually mean client-side JavaScript. This book documents client-side JavaScript, along with the core JavaScript language that client-side JavaScript incorporates.

當 JavaScript 解譯器嵌入 網頁瀏覽器時,就是用戶端 JavaScript 了。這是迄今為止最普通的 JavaScript 變體。當人們提到 JavaScript 時,通常所指的是用戶端 JavaScript。本書中的用戶端 JavaScript,它與 JavaScript 語言核心是混為一談的。

Client-side JavaScript combines the scripting ability of a JavaScript interpreter with the Document Object Model (DOM) defined by a web browser. Documents may contain JavaScript scripts, and those scripts can use the DOM to modify the document or control the web browser that displays the document. Put another way, we can say that client-side JavaScript adds behavior to otherwise static web content. Client-side JavaScript is at the heart of web development techniques such as DHTML (see Chapter 16) and architectures such as Ajax (see Chapter 20). The introduction to Chapter 13 includes an overview of the many capabilities of client-side JavaScript.

用戶端 JavaScript 將 JavaScript 指令碼編程的能力與 網頁瀏覽器中定義的 DOM 合二為一。文檔可含 JavaScript 指令碼,而這些指令碼可利用 DOM 修改文檔或控制 網頁瀏覽器顯示文檔。也就是說,用戶端 JavaScript 給靜態 web 內容層外加了一層額外的行為層。它是 web 開發技術的關鍵點(如 DHTML(見第16章))和架構結構(如 Ajax(見第20章))。在第13章節中概述了用戶端 JavaScript 的各個方面。

Just as the ECMA-262 specification defines a standard version of the core JavaScript language, the World Wide Web Consortium (W3C) has published a DOM specification that standardizes the features a browser must support in its DOM. (You'll learn much more about this standard in Chapters 15, 16, and 17.) The core portions of the W3C DOM standard are well supported in all major web browsers. One notable exception is Microsoft Internet Explorer, which does not support the W3C standard for event handling.

正如 ECMA-262 規範定義了 JavaScript 語言核心的標準化版本一樣,W3C也發布了DOM 規範,將瀏覽器必須其 DOM 所支援的特性進行了標準化。我們會在第15、16、17章節中瞭解更多的有關內容。雖然 W3C DOM 標準的核心部分得到了所有主流 網頁瀏覽器的支援,但是有點要注意的是,微軟的 IE不支援 W3C 標準的事件處理。

1.3.1. Client-Side JavaScript Examples1.3.1. 用戶端 JavaScript 舉例

When a web browser is augmented with a JavaScript interpreter, it allows executable content to be distributed over the Internet in the form of JavaScript scripts. Example 1-1 shows what this looks like: it is a simple JavaScript program, or script, embedded in an HTML file.

在內嵌 JavaScript 解譯器的 網頁瀏覽器中,可允許可執行內容以 JavaScript 指令碼的形式分布到 Internet 中。例 1-1 是簡單的在 HTML 檔案內的 JavaScript 程式(或稱指令碼)。

<html><head><title>Factorials</title></head><body><h2>Table of Factorials</h2><script>var fact = 1;for(i = 1; i < 10; i++) {    fact = fact*i;    document.write(i + "! = " + fact + "<br>");}</script></body></html>

When loaded into a JavaScript-enabled browser, this script produces the output shown in Figure 1-1.

在啟用 JavaScript 的瀏覽器中載入後,就會產生 1-1 所示的輸出結果。

Figure 1-1. A web page generated with JavaScript

圖 1-1 用了 JavaScript 所產生的網頁

As you can see in this example, the <script> and </script>tags are used to embed JavaScript code in an HTML file. I'll describe the <script> tag further in Chapter 13. The main feature of JavaScript demonstrated by this example is the use of the document.write() method.[*] This method is used to dynamically output HTML text into an HTML document while it is being loaded into the browser.

在這個例子中可以看到,<script> 與 </script> 標籤是將 JavaScript 代碼嵌到 HTML 檔案內。我們將會在第13章中瞭解到更多 <script> 標籤的內容。這個例子表明了 JavaScript 的一個主要功能: document.write() 方法(注1)的應用。該方法用於動態地將 HTML 文本輸出到被載入的瀏覽器,並顯示在 HTML 文檔中。

[*] "Method" is the OO term for function or procedure; you'll see it used throughout this book.

注1:方法是物件導向(OO)的術語,指函數或過程。在本書中會常看到它。

JavaScript can control not only the content of HTML documents but also the behavior of those documents. That is, a JavaScript program might respond in some way when you enter a value in an input field or hover the mouse over an image in a document. JavaScript does this by defining event handlers for the document pieces of JavaScript code that are executed when a particular event occurs, such as when the user clicks on a button. Example 1-2 shows a simple HTML fragment that includes an event handler executed in response to a button click.

JavaScript 不僅能控制 HTML 文檔的內容,而且也能控制這些文檔的行為。也就是說,在輸入地區中輸入值時,或是在文檔中把滑鼠移到了映像之上時,JavaScript 程式可做些反應。JavaScript由文檔的事件處理來實現,事件處理是指特定的事件發生時有段 JavaScript 代碼被執行,如使用者點擊了按鈕。例 1-2 是一段簡單的 HTML,響應到按鈕點擊便執行事件處理。

Example 1-2. An HTML button with a JavaScript event handler defined

例 1-2 帶 JavaScript 事件處理定義的HTML按鈕

<button onclick="alert('You clicked the button');">Click here</button>

Figure 1-2 illustrates the result of clicking the button.

圖 1-2 為點擊按鈕時的效果。

Figure 1-2. The JavaScript response to an event

The onclick attribute shown in Example 1-2 holds a string of JavaScript code that's executed when the user clicks the button. In this case, the onclick event handler calls the alert() function. As you can see in Figure 1-2, alert() pops up a dialog box to display the specified message.

上例中的 onclick 屬性內是 JavaScript 代碼,該代碼在使用者點擊按鈕時會被執行。 Onclick 事件處理調用了 alert() 函數。可在圖 1-2 中看到,alert() 彈出了一個對話方塊,其實顯示所指定的訊息。

Example 1-1 and 1-2 highlight only the simplest features of client-side JavaScript. The real power of JavaScript on the client side is that scripts have access to the content of HTML documents. Example 1-3 contains a complete, nontrivial JavaScript program. The program computes the monthly payment on a home mortgage or other loan, given the amount of the loan, the interest rate, and the repayment period. It reads user input from HTML form fields, performs computations on that input, and then alters the document to display the results of the computation.

例 1-1 和 1-2 只不過用了用戶端 JavaScript 最簡單的特性。JavaScript 在用戶端真正的威力是指令碼訪問 HTML 文檔的內容。例 1-3 是個完整的而不簡單的JavaScript程式。它計算了每月在房貸及其它借貸的花銷,可算得借貸的總額、利息率、還貸期限。它將使用者在HTML表單欄位中的輸入讀取,通過計算,然後將計算結果顯示在文檔中。

翻譯:《JavaScript 權威指南(第5版)》第一章(二)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.