ECMAScript 2015 標準解讀(1)

ECMAScript概述

ECMAScript is an object-oriented programming language for performing computations and manipulating computational objects within a host environment. react

ECMAScript是一門面向對象的(腳本)編程語言,它在特定的宿主環境中執行計算和處理可計算對象。程序員

這裏有幾個比較重要的點:web

  1. 面向對象
  2. 腳本語言
  3. 宿主環境

下面咱們來根據上面的關鍵詞依次作出解釋。express

面向對象

ECMAScript is object-based: basic language and host facilities are provided by objects, and an ECMAScript program is a cluster of communicating objects.編程

ECMAScript是基於對象的:基礎語言功能以及宿主設施以對象的方式提供,而且,ECMAScript程序自己就是可交互的對象集合。windows

腳本語言

A scripting language is a programming language that is used to manipulate, customize, and automate the facilities of an existing system. In such systems, useful functionality is already available through a user interface, and the scripting language is a mechanism for exposing that functionality to program control. In this way, the existing system is said to provide a host environment of objects and facilities, which completes the capabilities of the scripting language.數組

腳本語言是在已有系統中操做、自定義和自動化基礎設施的編程語言。在這個系統中,可用功能以用戶接口的方式提供給用戶,腳本語言自己就是暴露接口來控制程序的一種機制。就這樣,已有系統被稱爲提供對象以及其餘基礎設施的宿主環境,以此完成腳本語言的功能。瀏覽器

宿主環境

ECMAScript最初是被設計爲Web腳本語言的,它能夠在瀏覽器中操做Web頁面,也能夠做爲Web客戶端-服務器架構中的一部分執行服務端計算。安全

A web browser provides an ECMAScript host environment for client-side computation including, for instance, objects that represent windows, menus, pop-ups, dialog boxes, text areas, anchors, frames, history, cookies, and input/output. Further, the host environment provides a means to attach scripting code to events such as change of focus, page and image loading, unloading, error and abort, selection, form submission, and mouse actions. Scripting code appears within the HTML and the displayed page is a combination of user interface elements and fixed and computed text and images. The scripting code is reactive to user interaction and there is no need for a main program.bash

web瀏覽器提供了ECMAScript執行客戶端計算的宿主環境,包括:instance, objects that represent windows, menus, pop-ups, dialog boxes, text areas, anchors, frames, history, cookies, 以及input/output等對象。另外,宿主環境提供了一種將代碼與事件綁定的機制,例如改變焦點、頁面和圖片的加載卸載報錯終止、選擇提交以及鼠標動做等。腳本代碼能夠出如今HTML中,所以,咱們所看到的網頁實際上是用戶接口元素與文本圖片等的結合體。腳本代碼能夠響應用戶行爲,而且這個過程不須要主程序參與。

A web server provides a different host environment for server-side computation including objects representing requests, clients, and files; and mechanisms to lock and share data.

Web服務器提供了一個不一樣的宿主環境用來進行服務端計算,它包括如下對象:representing requests, clients 和 files;另外,它還提供了一種數據加鎖和數據分享的機制。

對象

在ECMAScript中,對象(Object)是零個或多個屬性(property)的集合,每一個屬性有描述其使用方式的特性(attributes)。咱們能夠把屬性看作是一個容器,能夠盛放其餘的對象、原始值以及函數。

ECMAScript defines a collection of built-in objects that round out the definition of ECMAScript entities.

ECMAScript定義了一系列的內置對象(build-in objects),他們構成了ECMAScript定義的實體。

這些內置對象包括:

  1. 全局對象 Global Object
  2. ECMAScript運行時基礎對象 Object, Function, Boolean, Symbol, 各類Error
  3. 數字日期處理 Math Number Date
  4. 字符串文本處理 String RegExp
  5. 數據集合 Array TypeArray Map Set
  6. 結構化數據對象 JSON ArrayBuffer DataView
  7. 對象的抽象控制 Generator Promise
  8. 對象反射 Proxy Reflect

原型鏈繼承

ECMAScript對象的繼承並非像Java、C++同樣基於類的繼承。對象能夠經過字面量符號(literal notation)或者構造器(constructor)建立。構造器是一個函數對象,每一個構造器都擁有一個prototype的屬性,該屬性用於實現基於原型的繼承以及屬性共享。構造器使用new操做符來完成對象建立工做。

Every object created by a constructor has an implicit reference (called the object’s prototype) to the value of its constructor’s "prototype" property. Furthermore, a prototype may have a non-null implicit reference to its prototype, and so on; this is called the prototype chain.

每個被構造器(constructorA)生成的對象A都會存在一個指向 constructorA.prototype 的隱式引用(ECMAScript標準中爲[[ prototype ]],該引用在Chrome瀏覽器中對應的實現爲__proto__),即A.__proto__指向constructorA.prototype;另外,constructorA.prototype自己也是對象(即對象B),可能由另外的構造器(constructorB)建立,即B.__proto__指向constructorB.prototype(對象O);以此類推,直到某個對象的__proto__指向空指針null,這就構成了咱們所說的原型鏈。

以上過程的示意圖以下:

原型鏈示意圖

嚴格模式

背景

ECMAScript從設計之初就以簡單易用爲理念,吸引了很大一批開發者。寬鬆的語言限制對入門ECMAScript是有好處的,但對於高級ECMAScript程序員來講,這無疑是「災難」。

嚴格模式是限制更多的ECMAScript

ECMAScript意識到了這樣的需求,而且以嚴格模式的方式讓開發者能夠更好地限制語言層面的一些奇怪的特性。

They might do so in the interests of security, to avoid what they consider to be error-prone features, to get enhanced error checking, or for other reasons of their choosing. In support of this possibility, ECMAScript defines a strict variant of the language. The strict variant of the language excludes some specific syntactic and semantic features of the regular ECMAScript language and modifies the detailed semantics of some features. The strict variant also specifies additional error conditions that must be reported by throwing error exceptions in situations that are not specified as errors by the non-strict form of the language.

一些開發者會更關注安全問題,爲了達到這一目的,他們一般會避免使用一些容易出錯的特性,加強代碼的錯誤檢測等等。爲了支持這一需求,ECMAScript在語言中定義了一種「嚴格變體」(嚴格模式)。這種嚴格模式除去了一些特定的語法特徵,而且修正了一些語義細節。另外,嚴格模式相比於非嚴格模式會在某些特定狀況下拋出錯誤而不是靜默失敗。

MDN中對嚴格模式有很詳細的使用說明:MDN嚴格模式

術語和定義

type 類型

數據類型

primitive value 原始值

Undefined Null Boolean Number Symbol String 六種數據類型的取值。

A primitive value is a datum that is represented directly at the lowest level of the language implementation.

原始值是最基本的數據,是語言最底層的實現。

object 對象

Object類型的成員

An object is a collection of properties and has a single prototype object. The prototype may be the null value.

  1. 對象是屬性的集合
  2. 每一個對象存在一個內部(隱式)屬性[[ prototype ]],指向另外的一個對象,咱們稱之爲原型對象
  3. 原型對象可能爲null

constructor 構造器

構造器是建立和初始化對象的 函數對象。

The value of a constructor’s prototype property is a prototype object that is used to implement inheritance and shared properties.

  1. 構造器是函數,函數也是對象
  2. 構造器存在一個顯式(外部)屬性prototype,該屬性是一個對象,prototype是實現繼承和屬性共享的基礎。

prototype 原型

原型可讓對象之間進行屬性共享。

When a constructor creates an object, that object implicitly references the constructor’s prototype property for the purpose of resolving property references. The constructor’s prototype property can be referenced by the program expression constructor.prototype, and properties added to an object’s prototype are shared, through inheritance, by all objects sharing the prototype. Alternatively, a new object may be created with an explicitly specified prototype by using the Object.create built-in function.

ordinary object 普通對象

做爲普通對象,它必須擁有全部對象都支持的一些關鍵內部方法,而且支持其默認行爲。

exotic object 外來對象

外來對象有一些關鍵的內部方法的表現可能與默認行爲會存在不一樣。

standard object

標準對象的語義符合ECMAScript規範

build-in object

內置對象是由"ECMAScript實現"(這裏能夠理解爲宿主對象或者爲JS引擎)指定和提供的對象。

Standard built-in objects are defined in this specification. An ECMAScript implementation may specify and supply additional kinds of built-in objects. A built-in constructor is a built-in object that is also a constructor.

標準內置對象是由ECMAScript規範所定義的。一個ECMAScript的實現可能會提供額外的內置對象。內置構造器是一個內置函數對象。

總結:

  1. 一個對象符合ECMAScript語義規範,它就能夠稱爲標準對象。
  2. ECMAScript實現 中除了ECMAScript規範中規定的內置對象,還提供了與宿主環境相關的內置對象,例如,瀏覽器環境存在location、document對象,這些也稱爲內置對象。
  3. 標準內置對象 則特指ECMAScript規範中所定義的內置對象。

undefined vaule

undefined 是一個原始類型值,當一個變量聲明後未賦值,它的取值是undefined

Undefined type

僅有一個undefined值得數據類型

null value

null是一個原始類型值,它表明一個空對象指針。

Null type

僅有一個null值得數據類型

Boolean value

Boolean類型的數據成員

There are only two Boolean values, true and false
複製代碼

Boolean type

包含兩個原始值 true false 的數據類型

Boolean object

標準內置構造器 Boolean 的實例對象。

A Boolean object is created by using the Boolean constructor in a new expression, supplying a Boolean value as an argument. The resulting object has an internal slot whose value is the Boolean value. A Boolean object can be coerced to a Boolean value.

String value

字符串值是一種原始值,它是一種由零個或多個16 bit無符號整數組成的有限的有序序列。

A String value is a member of the String type. Each integer value in the sequence usually represents a single 16-bit unit of UTF-16 text. However, ECMAScript does not place any restrictions or requirements on the values except that they must be 16-bit unsigned integers.

字符串值是字符串數據類型的組成成員。序列中的每個整數值一般表明一個16-bit的UTF-16字符集單元。然而,ECMAScript並不強制或要求它們必定是 16-bit 無符號整數。

String type

字符串數據類型是全部字符串取值的集合。

String object

字符串對象是標準內置構造器String的實例。

Number value

一種原始值數據,它的表現與IEEE 754-2008數值規範中的雙精度64-bit數值一致。

A Number value is a member of the Number type and is a direct representation of a number.

數字值是數字數據類型中 全部的數字

Number type

數字數據類型 包括 數字值 NaN(Not-a-Number) +Infinate -Infinate

Number object

數字對象是標準內置構造器Number的實例。

A Number object is created by using the Number constructor in a new expression, supplying a number value as an argument. The resulting object has an internal slot whose value is the number value. A Number object can be coerced to a number value by calling the Number constructor as a function (20.1.1.1).

NaN

IEEE-754-2008規範中的 「Not-a-Number」

Infinity

正無窮

Symbol value

一種原始值,它用於對象 非字符串類型的惟一鍵 (key)

Symbol type

全部符合值的集合

Symbol object

標準內置構造器 Symbol 的實例。

function

能夠做爲子程序調用的對象類型成員。

In addition to its properties, a function contains executable code and state that determine how it behaves when invoked. A function’s code may or may not be written in ECMAScript.

除了包含屬性外,一個函數還包括了可執行代碼和與調用行爲相關的狀態。函數代碼能夠不寫(空函數)。

build-in function

內置函數 例如parseInt Math.exp等

property

屬性是對象的一部分,它是一個關聯的鍵(String value 或者 Symbol value)值對組合

method

對象屬性是函數,此時稱這個屬性爲方法

build-in method

內置方法:該方法是一個內置函數

attribute

特性:它是一些內部值,定義了屬性的特徵。

own property

一個對象自身所包含的屬性 而非從原型鏈中繼承的屬性

inherited property

繼承屬性:從原型鏈中取得的屬性

相關文章
相關標籤/搜索