31. 配列の内包表記 (Comprehensions)
// 配列のフィルタとマップ
[for (x of [1,-4,5,3,-7]) if (x > 0) x] // -> [1, 5, 3]
// ES5 なら次のように書く
// [1,-4,5,3,-7].filter(function(x) { return x > 0 });
[for (x of [2,4,6]) x*x] // -> [4, 16, 36]
// ES5 なら次のように書く:
// [2,4,6].map(function (x) { return x*xi });
!
// 配列のデカルト積やラベル生成もシンプルに
[for (i of [0,2,4]) for (j of [5,3]) i*j]
// -> [0, 0, 10, 6, 20, 12]
[for (x of 'abc'.split(''))
for (y of '123'.split('')) (x+y)];
// -> ["a1","a2","a3","b1","b2","b3","c1","c2","c3"]
32. 配列の内包表記の構文変更
// 旧 ES6 構文 (現行 Firefox の実装)
// 値の定義が最初 (for や if の左)
return [a+b for (a of A) for (b of B) if (a > b)]
!
// 新 ES6 構文
// 値の定義は最後 (for や if の右)
return [for (a of A) for (b of B) if (a > b) a+b]
!
// 新構文の方が読みやすいので構文変更された
https://gist.github.com/dherman/b250d1fad15dbb5f77a5
35. let
{
// let 定義: ブロックスコープ
let a = 1, b = 10;
// let 式・文: let (...) に続く式・文中だけで有効
let (a = 100, c = 300) console.log(a); // -> 100
// for 文などでの let
for (let a=0; a<3; a++) {
console.log(a+b); // -> 10, 11, 12
}
console.log(a); // -> 1
}
console.log(a); // × ReferenceError: a is not
// defined
48. Arrow Function における this
// this は矢印関数を囲むスコープのものにバインド
// コールバック利用時に self=this とか不要になる
function Person(){
this.age = 0;
setInterval(() => {
this.age++; // this は Person オブジェクト
}, 1000);
}
var p = new Person();
!
// 注: strict mode でも this はレキシカルに bind
// 済みとして振る舞うので undefined にならない
https://developer.mozilla.org/docs/Web/JavaScript/Reference/arrow_functions
65. Note: JavaScript Good Parts
人間が誤認しにくいコード
意図せぬ挙動を防げる記法 (コー
ディング規約) を論理的に解説
Douglas Crockford 提唱
JSON の生みの親、JS 業界の重鎮
ECMAScript 4th を葬った人です
職業: The Boss of You