The Sixth Edition, initially known as ECMAScript 6 (ES6) and later renamed to ECMAScript 2015, adds significant new syntax for writing complex applications, including classes and modules, iterators and for/of loops, generators, arrow functions, binary data, typed arrays, collections (maps, sets and weak maps), promises, number and math enhancements, reflection, and proxies.
在第6版,原來被稱為ECMAScript 6(ES6),後來被改名為ECMAScript 2015,增加了重要的(significant)新語法來撰寫複雜的應用程式,包含類別和模組(classes and modules)、迭代器和for及of迴圈(iterators and for/of loops)、產生器(generators)、箭頭函式(arrow functions)、二元資料(binary data)、型別陣列(typed arrays)、集合(collections,包含maps、sets和weak maps)、promises非同步語法、數字和數學增強(enhancements)、反射(reflection)以及代理(proxies)。
In other words, ES6 is a superset of JavaScript (ES5). The reason that ES6 became so popular is that it introduced new conventions and OOP concepts such as classes.
TIP:In this module, we cover the most important additions to ES6. So, let's jump right in!
提示:在這個單元,我們介紹ES6最重要的新增規範,那麼,我們立刻開始吧!
-----
var & let
var和let (關鍵字)
In ES6 we have three ways of declaring variables:
在ES6我們有3種方式宣告變數:
var a = 10; const b = 'hello'; let c = true;
The type of declaration used depends on the necessary scope. Scope is the fundamental concept in all programming languages that defines the visibility of a variable.
var & let Unlike the var keyword, which defines a variable globally, or locally to an entire function regardless of block scope, let allows you to declare variables that are limited in scope to the block, statement, or expression in which they are used. For example:
const variables have the same scope as variables declared using let. The difference is that const variables are immutable - they are not allowed to be reassigned. For example, the following generates an exception:
const變數跟使用let宣告的變數有一樣的範圍,差別在於const變數是不可變的(immutable),他們不允許被重新分配(reassigned)。 下面的範例會產生反駁(exception): (Uncaught TypeError: Assignment to constant variable.)
const a = 'Hello'; a = 'Bye'
TIP:const is not subject to Variable Hoisting too, which means that const declarations do not move to the top of the current execution context. Also note that ES6 code will run only in browsers that support it. Older devices and browsers that do not support ES6 will return a syntax error.
let name = 'David'; let msg = 'Welcome ' + name + '!'; console.log(msg);
ES6 introduces a new way of outputting variable values in strings. The same code above can be rewritten as:
ES6引入在字串中印出變數值的全新方式,上方相同的程式碼可以改寫成:
let name = 'David'; let msg = `Welcome ${name}!`; console.log(msg);
Notice, that template literals are enclosed by the backtick (` `) character instead of double or single quotes. The ${expression} is a placeholder, and can include any expression, which will get evaluated and inserted into the template literal.
let a = 8; let b = 34; let msg = `The sum is ${a+b}`; console.log(msg);
TIP:To escape a backtick in a template literal, put a backslash \ before the backtick.
提示:要跳脫樣板字面值的反引號,可以放入反斜線\在反引號之前。
-----
翻譯小心得| 我開始學習JS時就是ES6的標準了,很幸運有前人的耕耘,所以避免很多標準不同的陣痛,基礎的概念不成問題,期待後面更深入的介紹。 Finally, the article above is from SOLOLEARN . I translated it just for practice, personal use only. If that is inappropriate, please contact me.
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.