總覽各版 ECMAScript 功能

JavaScriptECMAScriptECMAScript 2ECMAScript 3ECMAScript 4ECMAScript 5ECMAScript 2015

ECMAScript 2016ECMAScript 2017ECMAScript 2018ECMAScript 2019ECMAScript 2020ECMAScript 2021ECMAScript 2022ECMAScript 2023

ECMAScript LatestMy ECMAScript now


JavaScript

網際網路迅速崛起的時代,最惱人的使用者體驗,就是提交資料給某個全球資訊網後,網速慢等了快十秒,總算看到下一頁時,竟然是回報填寫資料不正確的網頁。於是 Netscape Communications 研擬 Mocha 計畫,希望瀏覽器本身就能跑簡單的程式,分擔伺服器的工作,在遞交資料給伺服器前,瀏覽器自己就能先檢查資料。

Netscape Communications 委派 Brendan Eich 開發一款新的程式語言,目標是能將原始碼直接寫在網頁裡面,而且沒寫過程式的人也能使用。

沒幾天,Netscape Communications 對 HotJava 瀏覽器的 Applet 驚喜不已,決定與 Sun Microsystems 洽談,想讓自己的瀏覽器也能支援 Applet。雙方協議合作後,給 Brendan Eich 加入一個要求:「讓開發中的程式語言長得像 Java 吧!」

Brendan Eich 參考 Self 程式語言的原型機制,以及 Scheme 程式語言的一級函數,只花約十天的時間,完成了一款弱型別的直譯式程式語言交差了事。

1995 年 9 月先以 LiveScript 的名稱,搭載於 Netscape Navigator 2.0 測試版,12 月正式版時改名為 JavaScript。(至於 Mocha 即後來的 SpiderMonkey 引擎。)


ECMAScript

JavaScript 發明之初,只是一款可以操作些瀏覽器軟體本身的指令語言,簡簡單單一兩行程式,無需專業程式設計師背景,就能動態變化瀏覽器內容,顯著可見的效果讓 JavaScript 大受好評!大家都搶著在網頁寫個 JavaScript 來露一下身手:


於是 1996 年 8 月 Microsoft 趕緊也抄了一個 JScript 來應戰,推出 Internet Explorer 3.0。

1996 年 11 月,Netscape Communications 將 JavaScript 提交給 Ecma International,制定統一規範,專案代號為 ECMA-262。由於 JavaScript 有 Java 字樣,Sun Microsystems 為保護 Java 版權,在一起合作的 Netscape Communications 同意下,將 JavaScript 註冊商標,因此 Ecma International 另外取名為 ECMAScript。

1997 年 6 月,ECMAScript 1.0 標準化正式定案。


ECMAScript 2

1998 年 6 月,修正規格書,以適用 ISO/IEC16262 標準化,發表 ECMAScript 2.0。


ECMAScript 3

於 1999 年 12 月發布。

語言方面,更改了字串的內部設計、新增資料的格式化輸出、流程控制、例外處理等功能。

API 方面內建 Regular expressions,並為該算式設計專屬字串符號 / /


Structured programming


579


Object oriented programming


112
223


Prototype programming


Hello
Hello, world!


Closure with first class function


579
579
222
444


ECMAScript 4

2007 年 10 月時發布草案後,因為語法躍進幅度太大,與其說更新標準,不如說在汰舊標準,因此意見分歧,已到無法達成共識的地步,因此於 2008 年 8 月放棄,直接跳到 ECMAScript 5。

Macromedia Flash 的 ActionScript 實作了 ECMAScript 4 的規格!因此好奇這是怎樣語法的話,可以藉由 ActionScript 一探究竟~

基本上從 ActionScript 來看,ECMAScript 4 確實太笨重了:「程式碼寫起來有點像 Java 那樣囉嗦。」做為表述語言1,訴求是隨手就能寫一段程式來跑,而不是管理好大量程式碼才能跑,這種嚴重破壞 JavaScript 寫作效率的語言規範,確實沒有通過的必要。

或許現在回頭看,靜態型別風格的 ECMAScript 4 規範得並不差,甚至令人欣賞與惋惜。但它只適合實作在編譯式的程式語言,拋出越多語法錯誤越好,像是 TypeScript,這語言能拋出精準、有用的訊息,在開發大型專案時幫助很大。直譯式的 JavaScript 則不適合,應該盡量容錯,越少錯誤越好~


ECMAScript 5

於 2009 年 12 月正式發布,離上一次版本有十年之久。

由於接下來的 ECMAScript 2015 會有重大改變,因此 ECMAScript 5 主要特性不是更新語言,而是在相容 ECMAScript 3 的基礎上,導入 ECMAScript 4 裡面已被認可的新觀念,引導使用者將來能銜接到 ECMAScript 2015。

ECMAScript 5 基本上只在 API 引進 ECMAScript 4 的新觀念(原型物件、擁抱函數式程式設計、遠離物件導向的類別),然後透過 strict mode(嚴格模式)彌補 ECMAScript 4 想解決沒有資料型態與區域變數所帶來的問題。

總之,ECMAScript 5 最大的意義,就是 ECMAScript 3 的最終版本,為舊時代的技術進行最後的完善,使其在未來能延用下去。ECMAScript 2015 最大的意義,則是 ECMAScript 4 的修正版本,為新時代的開創做技術上的準備,填補過去的不足。


Object literal


123
456
123


Array structure object


789
987
789


ECMAScript 2015 (ECMA-262 6th Edition)

於 2015 年 6 月正式發布!由於等功能全到位才制定新規範被嫌速度太慢,技術委員會3 決定往後採滾動發行,每年進行小幅度功能更新,所以這版不稱 ECMAScript 6,改用年份做為版號~

ECMAScript 2015 一方面引進 ECMAScript 4 的新功能,一方面將功能設計成符合動態型別的語法,避免靜態型別的風格破壞 ECMAScript 優點。新的語法,可以把程式寫得完全不像過去 ECMAScript 3 的程式,像新的語言一樣,但寫這樣的程式,效率是大幅提高的,而不是降低,可謂設計得非常成功!

可塑性也更高了!能用類似 Python 的寫法設計程式,也能繼續用 Java 和 C 風格寫程式,甚至像 Java 在模仿 Python 的方式寫程式也可以,讓更多人能用自己的習慣寫 JavaScript 程式,更好上手了!

這次新增 classconstexportextendsimportletofyield 關鍵字,以及 Set、Map、Promise、Fetch 物件功能,還有大量語法糖。詳細功能範例,請參考:http://es6-features.org


Scope


Uncaught SyntaxError: Identifier 'a' has already been declared

123
Uncaught ReferenceError: b is not defined


Classes


10
123
81
111222333
456


Module system

module.js

test1.js

test2.js


Template literal

Multi line string


abc 123

Expression interpolation


AAA BBB CCC


Destructuring assignment


AAA
BBB
CCC


Default parameter


912
579


Rest parameter


123 456
123 456,789


Arrow function


112
223
334
445
555


Method definitition


ABC


Generators


9
18
36
72


Symbol


Twideem Civs
58

如果 data[] 裡面的鍵值未定義,例如 data[position],會回報錯誤。


ECMAScript 2016 (ECMA-262 7th Edition)

ECMAScript 2016 是採用滾動發行後第一次工作結果…更新幅度小到讓人不覺得有必要發行獨立的版本 XDDD

這政策的改變,是因為技術委員會的成員結構發生改變,裡面一堆網際網路大廠的人馬,所以不見得是好事~

做為一個標準化規範,每個版本都應該深思熟慮,經過下游業者再三試驗,確認這是 ECMAScript 需要的,才放行。現在以求新求變的節奏發布功能,不再去看下游業者使用的情況如何,而是技術委員會的上游廠商人馬自己內部說了算,ECMAScript language specification 變成為瀏覽器廠商背書用的:「請用這些功能,讓我們大廠好做事。」

不過,滾動式發行的好處還是大於壞處!哪一項功能是業界最迫切需要的,明年就針對這項目做出規範,例如下一版的 async 和 await 就來得很是時候,及時把最重要的部分,提早規範出來給業界遵循和使用。

ECMAScript 2016 was the first ECMAScript edition released under Ecma TC39's new yearly release cadence and open development process. A plain-text source document was built from the ECMAScript 2015 source document to serve as the base for further development entirely on GitHub. Over the year of this standard's development, hundreds of pull requests and issues were filed representing thousands of bug fixes, editorial fixes and other improvements. Additionally, numerous software tools were developed to aid in this effort including Ecmarkup, Ecmarkdown, and Grammarkdown. ES2016 also included support for a new exponentiation operator and adds a new method to Array.prototype called includes.


ECMAScript 2017 (ECMA-262 8th Edition)

ECMAScript 2017 introduced Async Functions, Shared Memory, and Atomics along with smaller language and library enhancements, bug fixes, and editorial updates. Async functions improve the asynchronous programming experience by providing syntax for promise-returning functions. Shared Memory and Atomics introduce a new memory model that allows multi-agent programs to communicate using atomic operations that ensure a well-defined execution order even on parallel CPUs. It also included new static methods on Object: Object.values, Object.entries, and Object.getOwnPropertyDescriptors.


ECMAScript 2018 (ECMA-262 9th Edition)

ECMAScript 2018 introduced support for asynchronous iteration via the AsyncIterator protocol and async generators. It also included four new regular expression features: the dotAll flag, named capture groups, Unicode property escapes, and look-behind assertions. Lastly it included object rest and spread properties.


ECMAScript 2019 (ECMA-262 10th Edition)

ECMAScript 2019 introduced a few new built-in functions: flat and flatMap on Array.prototype for flattening arrays, Object.fromEntries for directly turning the return value of Object.entries into a new Object, and trimStart and trimEnd on String.prototype as better-named alternatives to the widely implemented but non-standard String.prototype.trimLeft and trimRight built-ins. In addition, it included a few minor updates to syntax and semantics. Updated syntax included optional catch binding parameters and allowing U+2028 (LINE SEPARATOR) and U+2029 (PARAGRAPH SEPARATOR) in string literals to align with JSON. Other updates included requiring that Array.prototype.sort be a stable sort, requiring that JSON.stringify return well-formed UTF-8 regardless of input, and clarifying Function.prototype.toString by requiring that it either return the corresponding original source text or a standard placeholder.


ECMAScript 2020 (ECMA-262 11th Edition)

ECMAScript 2020, the 11th edition, introduces the matchAll method for Strings, to produce an iterator for all match objects generated by a global regular expression; import(), a syntax to asynchronously import Modules with a dynamic specifier; BigInt, a new number primitive for working with arbitrary precision integers; Promise.allSettled, a new Promise combinator that does not short-circuit; globalThis, a universal way to access the global this value; dedicated export*as ns from 'module' syntax for use within modules; increased standardization of for-in enumeration order; import.meta, a host-populated object available in Modules that may contain contextual information about the Module; as well as adding two new syntax features to improve working with “nullish” values (null or undefined): nullish coalescing, a value selection operator; and optional chaining, a property access and function invocation operator that short-circuits if the value to access/invoke is nullish.


ECMAScript 2021 (ECMA-262 12th Edition)

This specification, the 12th edition, introduces the replaceAll method for Strings; Promise.any, a Promise combinator that short-circuits when an input value is fulfilled; AggregateError, a new Error type to represent multiple errors at once; logical assignment operators (??=, &&=, ||=); WeakRef, for referring to a target object without preserving it from garbage collection, and FinalizationRegistry, to manage registration and unregistration of cleanup operations performed when target objects are garbage collected; separators for numeric literals (1_000); and Array.prototype.sort was made more precise, reducing the amount of cases that result in an implementation-defined sort order.


ECMAScript 2022 (ECMA-262 13th Edition)

ECMAScript 2022, the 13th edition, introduced top-level await, allowing the keyword to be used at the top level of modules; new class elements: public and private instance fields, public and private static fields, private instance methods and accessors, and private static methods and accessors; static blocks inside classes, to perform per-class evaluation initialization; the #x in obj syntax, to test for presence of private fields on objects; regular expression match indices via the /d flag, which provides start and end indices for matched substrings; the cause property on Error objects, which can be used to record a causation chain in errors; the at method for Strings, Arrays, and TypedArrays, which allows relative indexing; and Object.hasOwn, a convenient alternative to Object.prototype.hasOwnProperty.


ECMAScript 2023 (ECMA-262 14th Edition)

ECMAScript 2023, the 14th edition, introduced the toSorted, toReversed, with, findLast, and findLastIndex methods on Array.prototype and TypedArray.prototype, as well as the toSpliced method on Array.prototype; added support for #! comments at the beginning of files to better facilitate executable ECMAScript files; and allowed the use of most Symbols as keys in weak collections.