JavaScript ES6 語法說明
ES6 = ECMAScript 6 = ECMAScript 2015.
ES6 還是未有直接support class的 private member.
當使用const/let等時, 且在node.js上, 則一定要運行在Strict Mode Code下, 即use strict
. 不然會出現 Block-scoped declarations (let, const, function, class) not yet supported outside strict mode
ES6 如何在class上模擬private data/method:
- http://www.2ality.com/2016/01/private-data-classes.html
- http://stackoverflow.com/questions/27849064/how-to-implement-private-method-in-es6-class-with-traceur
上述兩個link都提到兩種方式WeakMap & Symbol.
ES6裡的Prototype
class
本質上是prototype的syntactical sugar)的導入. [https://developer.mozilla.org/en-US/docs/Web/JavaScript/Inheritance_and_the_prototype_chain]
Since ECMAScript 6, the [[Prototype]] is accessed using the accessors Object.getPrototypeOf() and Object.setPrototypeOf().
let , const 為 block level 變數的關鍵字
if(true){
var x = 1;
}
console.log(x); //1
if(true){
let y = 2; //its lifetime is only in thie scope
}
console.log(y); //exception, undefined
shorthand syntax of Method definitions
Given the following code:
var obj = {
foo: function() {},
bar: function() {}
};
You are now able to shorten this to:
var obj = {
foo() {},
bar() {}
};
其餘部份待補充, 可先參考
https://github.com/lukehoban/es6features
上面網站裡的功能清單 ES6 includes the following new features:
- arrows
- classes
- enhanced object literals
- template strings
- destructuring
- default + rest + spread
- let + const
- iterators + for..of
- generators
- unicode
- modules
- module loaders
- map + set + weakmap + weakset
- proxies
- symbols
- subclassable built-ins
- promises
- math + number + string + array + object APIs
- binary and octal literals
- reflect api
- tail calls