node v6.9.0が新しいLTSライン(Boron)としてリリースされた。
Node v6.9.0 (LTS) | Node.js
v4系列からアップグレードするにあたって、大きな変更点は
その他はリンク先を参照。
node v6系では import を除くECMAScript 2015 の仕様がほぼ網羅されている。さらにES2016のArray#includesも使用できる。
The spread operator for arrays and function calls(スプレッド演算子)
const arr = [1, 2, 3];
function hoge(a,b,c) {
return a + b + c;
}
hoge(...a);
const arr2 = [...a, 4, 5];
Rest parameters(可変長引数)
function hoge(a, ...b) {
return b.reduce((s, i) => s + i, a);
}
hoge(1, 2, 3, 4, 5);
Default function parameters(デフォルト引数)
function hoge(a = 1, b = 2, c = 3) {
return a + b + c;
}
hoge(0)
hoge(0, 0)
hoge(0, 0, 0)
Destructuring(分割代入)
const arr = [1, 2, 3, 4, 5];
const [a, b, ...rest] = arr;
const [c, d, e, f = 6] = rest;
const obj = {a: 1, b: 2};
const {a, b} = obj;
const {a: x, b: y} = obj;
const {a: p, b: q, c: r = 3} = obj;
プロキシ(とリフレクション)
const obj = {a: 1, b: 2, f: function() { return this.a + this.b }};
const p = new Proxy(obj, {
get(target, property, receiver) {
console.log('access property: ' + property);
return target[property];
}
});
console.log(p.f());
class Hoge {
constructor(a) {
this.a = a;
}
}
const hoge = Reflect.construct(Hoge, [1]);
console.log(hoge instanceof Hoge);
console.log(hoge.a);
Array#includes
const arr = [1, 2, 3];
if (arr.includes(1)) {
console.log('配列に1が含まれている');
}
Bufferコンストラクタが非推奨に
v6系では、セキュリティ上の理由からnew Buffer()が非推奨になっている。代わりに今後は Buffer.allocとBuffer.from を使えとのこと。