學習 ES2015
本文檔最初取自 Luke Hoban 出色的 es6features 儲存庫。快去 GitHub 上給它一個星星吧!
請務必在線上 REPL 中嘗試這些功能。
簡介
ECMAScript 2015 是一項於 2015 年 6 月通過的 ECMAScript 標準。
ES2015 是這項語言的重要更新,也是自 2009 年 ES5 標準化以來首次進行的重大更新。目前正在 進行 主要 JavaScript 引擎中這些功能的實作。
請參閱 ES2015 標準 以取得 ECMAScript 2015 語言的完整規格。
ECMAScript 2015 功能
箭頭函式和詞彙 this
箭頭函式是一種使用 =>
語法的函式簡寫。它們在語法上類似於 C#、Java 8 和 CoffeeScript 中的相關功能。它們支援表達式和陳述主體。與函式不同,箭頭函式與其周圍程式碼共用相同的詞彙 this
。如果箭頭函式在另一個函式內部,它會共用其父函式的「引數」變數。
// Expression bodies
var odds = evens.map(v => v + 1);
var nums = evens.map((v, i) => v + i);
// Statement bodies
nums.forEach(v => {
if (v % 5 === 0)
fives.push(v);
});
// Lexical this
var bob = {
_name: "Bob",
_friends: [],
printFriends() {
this._friends.forEach(f =>
console.log(this._name + " knows " + f));
}
};
// Lexical arguments
function square() {
let example = () => {
let numbers = [];
for (let number of arguments) {
numbers.push(number * number);
}
return numbers;
};
return example();
}
square(2, 4, 7.5, 8, 11.5, 21); // returns: [4, 16, 56.25, 64, 132.25, 441]
類別
ES2015 類別是基於原型 OO 模式的語法糖。具有一個單一的方便宣告形式,讓類別模式更容易使用,並鼓勵互操作性。類別支援基於原型的繼承、super 呼叫、執行個體和靜態方法以及建構函式。
class SkinnedMesh extends THREE.Mesh {
constructor(geometry, materials) {
super(geometry, materials);
this.idMatrix = SkinnedMesh.defaultMatrix();
this.bones = [];
this.boneMatrices = [];
//...
}
update(camera) {
//...
super.update();
}
static defaultMatrix() {
return new THREE.Matrix4();
}
}
增強的物件文字
物件文字擴充了建構時設定原型、簡寫 foo: foo
指派、定義方法和建立超呼叫的功能。這些功能讓物件文字和類別宣告更接近,並讓物件導向設計受益於一些相同的便利性。
var obj = {
// Sets the prototype. "__proto__" or '__proto__' would also work.
__proto__: theProtoObj,
// Computed property name does not set prototype or trigger early error for
// duplicate __proto__ properties.
['__proto__']: somethingElse,
// Shorthand for ‘handler: handler’
handler,
// Methods
toString() {
// Super calls
return "d " + super.toString();
},
// Computed (dynamic) property names
[ "prop_" + (() => 42)() ]: 42
};
樣板字串
樣板字串提供語法糖來建構字串。這類似於 Perl、Python 等語言的字串內插功能。另外,可以新增標籤以自訂字串建構,避免注入攻擊或從字串內容建構更高層級的資料結構。
// Basic literal string creation
`This is a pretty little template string.`
// Multiline strings
`In ES5 this is
not legal.`
// Interpolate variable bindings
var name = "Bob", time = "today";
`Hello ${name}, how are you ${time}?`
// Unescaped template strings
String.raw`In ES5 "\n" is a line-feed.`
// Construct an HTTP request prefix is used to interpret the replacements and construction
GET`http://foo.org/bar?a=${a}&b=${b}
Content-Type: application/json
X-Credentials: ${credentials}
{ "foo": ${foo},
"bar": ${bar}}`(myOnReadyStateChangeHandler);
解構
解構允許使用樣式比對進行繫結,並支援比對陣列和物件。解構是失敗軟性的,類似於標準物件查詢 foo["bar"]
,在找不到時會產生 undefined
值。
// list matching
var [a, ,b] = [1,2,3];
a === 1;
b === 3;
// object matching
var { op: a, lhs: { op: b }, rhs: c }
= getASTNode()
// object matching shorthand
// binds `op`, `lhs` and `rhs` in scope
var {op, lhs, rhs} = getASTNode()
// Can be used in parameter position
function g({name: x}) {
console.log(x);
}
g({name: 5})
// Fail-soft destructuring
var [a] = [];
a === undefined;
// Fail-soft destructuring with defaults
var [a = 1] = [];
a === 1;
// Destructuring + defaults arguments
function r({x, y, w = 10, h = 10}) {
return x + y + w + h;
}
r({x:1, y:2}) === 23
預設值 + 剩餘值 + 擴散
由被呼叫者評估的預設參數值。在函式呼叫中將陣列轉換為連續參數。將尾隨參數繫結到陣列。Rest 取代了對 arguments
的需求,並更直接地解決常見情況。
function f(x, y=12) {
// y is 12 if not passed (or passed as undefined)
return x + y;
}
f(3) == 15
function f(x, ...y) {
// y is an Array
return x * y.length;
}
f(3, "hello", true) == 6
function f(x, y, z) {
return x + y + z;
}
// Pass each elem of array as argument
f(...[1,2,3]) == 6
Let + Const
區塊範圍繫結建構。let
是新的 var
。const
是單一指定。靜態限制防止在指定前使用。
function f() {
{
let x;
{
// this is ok since it's a block scoped name
const x = "sneaky";
// error, was just defined with `const` above
x = "foo";
}
// this is ok since it was declared with `let`
x = "bar";
// error, already declared above in this block
let x = "inner";
}
}
迭代器 + For..Of
迭代器物件啟用自訂迭代,例如 CLR IEnumerable 或 Java Iterable。使用 for..of
將 for..in
概括為基於自訂迭代器的迭代。不需要實作陣列,啟用 LINQ 等延遲設計模式。
let fibonacci = {
[Symbol.iterator]() {
let pre = 0, cur = 1;
return {
next() {
[pre, cur] = [cur, pre + cur];
return { done: false, value: cur }
}
}
}
}
for (var n of fibonacci) {
// truncate the sequence at 1000
if (n > 1000)
break;
console.log(n);
}
迭代基於這些鴨子型別介面(僅使用 TypeScript 型別語法說明)
interface IteratorResult {
done: boolean;
value: any;
}
interface Iterator {
next(): IteratorResult;
}
interface Iterable {
[Symbol.iterator](): Iterator
}
若要使用迭代器,您必須包含 Babel 多重載入。
產生器
產生器使用 function*
和 yield
簡化迭代器建構。宣告為 function* 的函式會傳回產生器執行個體。產生器是迭代器的子型別,其中包含其他 next
和 throw
。這些可讓值回傳到產生器,因此 yield
是傳回值(或擲回)的表達式形式。
注意:也可使用來啟用類似「await」的非同步程式設計,另請參閱 ES7 await
建議。
var fibonacci = {
[Symbol.iterator]: function*() {
var pre = 0, cur = 1;
for (;;) {
var temp = pre;
pre = cur;
cur += temp;
yield cur;
}
}
}
for (var n of fibonacci) {
// truncate the sequence at 1000
if (n > 1000)
break;
console.log(n);
}
產生器介面是(僅使用 TypeScript 類型語法說明)
interface Generator extends Iterator {
next(value?: any): IteratorResult;
throw(exception: any);
}
若要使用產生器,您必須包含 Babel polyfill。
理解
在 Babel 6.0 中移除
Unicode
非中斷新增,以支援完整 Unicode,包括字串中的新 unicode 文字形式和新的 RegExp u
模式來處理碼點,以及處理 21 位元碼點層級字串的新 API。這些新增功能支援在 JavaScript 中建置全球應用程式。
// same as ES5.1
"𠮷".length == 2
// new RegExp behaviour, opt-in ‘u’
"𠮷".match(/./u)[0].length == 2
// new form
"\u{20BB7}" == "𠮷"
"𠮷" == "\uD842\uDFB7"
// new String ops
"𠮷".codePointAt(0) == 0x20BB7
// for-of iterates code points
for(var c of "𠮷") {
console.log(c);
}
模組
元件定義的模組語言層級支援。編纂來自熱門 JavaScript 模組載入器(AMD、CommonJS)的模式。由主機定義的預設載入器定義執行時期行為。隱含非同步模型 – 在要求的模組可用並處理之前,不會執行任何程式碼。
// lib/math.js
export function sum(x, y) {
return x + y;
}
export var pi = 3.141593;
// app.js
import * as math from "lib/math";
console.log("2π = " + math.sum(math.pi, math.pi));
// otherApp.js
import {sum, pi} from "lib/math";
console.log("2π = " + sum(pi, pi));
一些其他功能包括 export default
和 export *
// lib/mathplusplus.js
export * from "lib/math";
export var e = 2.71828182846;
export default function(x) {
return Math.exp(x);
}
// app.js
import exp, {pi, e} from "lib/mathplusplus";
console.log("e^π = " + exp(pi));
Babel 可以將 ES2015 模組轉譯成多種不同的格式,包括 Common.js、AMD、System 和 UMD。您甚至可以建立自己的格式。如需更多詳細資訊,請參閱 模組文件。
模組載入器
這在 ECMAScript 2015 規範中留待實作定義。最終的標準將會在 WHATWG 的 Loader 規範 中,但目前仍在進行中。以下內容來自先前的 ES2015 草稿。
模組載入器支援
- 動態載入
- 狀態隔離
- 全域命名空間隔離
- 編譯掛鉤
- 巢狀虛擬化
可以設定預設的模組載入器,也可以建構新的載入器,在隔離或受限的環境中評估和載入程式碼。
// Dynamic loading – ‘System’ is default loader
System.import("lib/math").then(function(m) {
alert("2π = " + m.sum(m.pi, m.pi));
});
// Create execution sandboxes – new Loaders
var loader = new Loader({
global: fixup(window) // replace ‘console.log’
});
loader.eval("console.log(\"hello world!\");");
// Directly manipulate module cache
System.get("jquery");
System.set("jquery", Module({$: $})); // WARNING: not yet finalized
由於 Babel 預設使用 common.js 模組,因此不包含模組載入器 API 的 polyfill。請從 這裡 取得。
要使用這個,你需要告訴 Babel 使用 system
模組格式器。另外,請務必查看 System.js。
Map + Set + WeakMap + WeakSet
常見演算法的高效資料結構。WeakMaps 提供無外洩的物件金鑰側邊表格。
// Sets
var s = new Set();
s.add("hello").add("goodbye").add("hello");
s.size === 2;
s.has("hello") === true;
// Maps
var m = new Map();
m.set("hello", 42);
m.set(s, 34);
m.get(s) == 34;
// Weak Maps
var wm = new WeakMap();
wm.set(s, { extra: 42 });
wm.size === undefined
// Weak Sets
var ws = new WeakSet();
ws.add({ data: 42 });
// Because the added object has no other references, it will not be held in the set
若要在所有環境中支援 Maps、Sets、WeakMaps 和 WeakSets,您必須包含 Babel polyfill。
Proxies
Proxies 能夠建立具有主機物件所有行為範圍的物件。可使用於攔截、物件虛擬化、記錄/剖析等。
// Proxying a normal object
var target = {};
var handler = {
get: function (receiver, name) {
return `Hello, ${name}!`;
}
};
var p = new Proxy(target, handler);
p.world === "Hello, world!";
// Proxying a function object
var target = function () { return "I am the target"; };
var handler = {
apply: function (receiver, ...args) {
return "I am the proxy";
}
};
var p = new Proxy(target, handler);
p() === "I am the proxy";
所有執行階段層級的元操作都有陷阱可用
var handler =
{
// target.prop
get: ...,
// target.prop = value
set: ...,
// 'prop' in target
has: ...,
// delete target.prop
deleteProperty: ...,
// target(...args)
apply: ...,
// new target(...args)
construct: ...,
// Object.getOwnPropertyDescriptor(target, 'prop')
getOwnPropertyDescriptor: ...,
// Object.defineProperty(target, 'prop', descriptor)
defineProperty: ...,
// Object.getPrototypeOf(target), Reflect.getPrototypeOf(target),
// target.__proto__, object.isPrototypeOf(target), object instanceof target
getPrototypeOf: ...,
// Object.setPrototypeOf(target), Reflect.setPrototypeOf(target)
setPrototypeOf: ...,
// Object.keys(target)
ownKeys: ...,
// Object.preventExtensions(target)
preventExtensions: ...,
// Object.isExtensible(target)
isExtensible :...
}
由於 ES5 的限制,Proxies 無法轉譯或 polyfill。請參閱 各種 JavaScript 引擎 中的支援。
符號
符號能控制物件狀態的存取。符號允許屬性以 字串
(如 ES5) 或 符號
為金鑰。符號是一種新的基本型別。選擇性的 name
參數用於除錯,但不是身分的一部分。符號是唯一的 (如 gensym),但由於它們透過反射功能 (如 Object.getOwnPropertySymbols
) 曝光,因此不是私密的。
(function() {
// module scoped symbol
var key = Symbol("key");
function MyClass(privateData) {
this[key] = privateData;
}
MyClass.prototype = {
doStuff: function() {
... this[key] ...
}
};
// Limited support from Babel, full support requires native implementation.
typeof key === "symbol"
})();
var c = new MyClass("hello")
c["key"] === undefined
可繼承內建物件
在 ES2015 中,內建物件(例如 Array
、Date
和 DOM Element
)可以繼承。
// User code of Array subclass
class MyArray extends Array {
constructor(...args) { super(...args); }
}
var arr = new MyArray();
arr[1] = 12;
arr.length == 2
內建物件的可繼承性應逐案評估,因為像 HTMLElement
等類別可以繼承,而許多像 Date
、Array
和 Error
等類別則不能繼承,這是因為 ES5 引擎的限制。
Math + Number + String + Object API
許多新的函式庫新增功能,包括核心 Math 函式庫、陣列轉換輔助程式,以及用於複製的 Object.assign。
Number.EPSILON
Number.isInteger(Infinity) // false
Number.isNaN("NaN") // false
Math.acosh(3) // 1.762747174039086
Math.hypot(3, 4) // 5
Math.imul(Math.pow(2, 32) - 1, Math.pow(2, 32) - 2) // 2
"abcde".includes("cd") // true
"abc".repeat(3) // "abcabcabc"
Array.from(document.querySelectorAll("*")) // Returns a real Array
Array.of(1, 2, 3) // Similar to new Array(...), but without special one-arg behavior
[0, 0, 0].fill(7, 1) // [0,7,7]
[1,2,3].findIndex(x => x == 2) // 1
["a", "b", "c"].entries() // iterator [0, "a"], [1,"b"], [2,"c"]
["a", "b", "c"].keys() // iterator 0, 1, 2
["a", "b", "c"].values() // iterator "a", "b", "c"
Object.assign(Point, { origin: new Point(0,0) })
二進位和八進位字面值
新增兩種數字文字形式,分別用於二進制 (b
) 和八進制 (o
)。
0b111110111 === 503 // true
0o767 === 503 // true
Babel 只轉換 0o767
,不轉換 Number("0o767")
。
Promises
Promises 是非同步程式設計的函式庫。Promises 是可能在未來提供的值的頭等表示形式。Promises 用於許多現有的 JavaScript 函式庫。
function timeout(duration = 0) {
return new Promise((resolve, reject) => {
setTimeout(resolve, duration);
})
}
var p = timeout(1000).then(() => {
return timeout(2000);
}).then(() => {
throw new Error("hmm");
}).catch(err => {
return Promise.all([timeout(100), timeout(200)]);
})
若要支援 Promises,您必須包含 Babel polyfill。
Reflect API
完整的 reflection API,公開物件的執行階段 meta 操作。這實際上是 Proxy API 的反向,允許呼叫與 proxy traps 相同的 meta 操作。對於實作 proxy 特別有用。
var O = {a: 1};
Object.defineProperty(O, 'b', {value: 2});
O[Symbol('c')] = 3;
Reflect.ownKeys(O); // ['a', 'b', Symbol(c)]
function C(a, b){
this.c = a + b;
}
var instance = Reflect.construct(C, [20, 22]);
instance.c; // 42
若要使用 Reflect API,您必須包含 Babel polyfill。
尾端呼叫
保證尾端位置的呼叫不會無限增加堆疊。讓遞迴演算法在面對無限制輸入時安全。
function factorial(n, acc = 1) {
"use strict";
if (n <= 1) return acc;
return factorial(n - 1, n * acc);
}
// Stack overflow in most implementations today,
// but safe on arbitrary inputs in ES2015
factorial(100000)
由於在全球支援尾呼叫的複雜性和效能影響,因此僅支援明確的自參照尾遞迴。由於其他錯誤而移除,並將重新實作。