Node.js v0.12で使えるようになるES6+の機能一覧

D76231a2114896dfcc7b79ac69558b79?s=47 Yosuke Furukawa
October 29, 2014
100k

Node.js v0.12で使えるようになるES6+の機能一覧

ES6+カジュアルトーク資料です!!

D76231a2114896dfcc7b79ac69558b79?s=128

Yosuke Furukawa

October 29, 2014
Tweet

Transcript

  1. Node.js v0.12Ͱ࢖͑ΔΑ͏ ʹͳΔES6+ͷػೳҰཡ @yosuke_furukawa

  2. @yosuke_furukawa Node.jsϢʔβʔάϧʔϓ୅ද / DeNAॴଐ

  3. Node.js v0.12 (ES6ରԠঢ়گ)

  4. Node.js ͷ v0.12ϒϥϯνΛجʹϏϧυͯ֬͠ೝ ͪͳΈʹv0.12ͷݱ࣌఺Ͱͷv8ͷ
 ࠾༻όʔδϣϯ͸3.28

  5. v0.12Ͱ࡟আ͞ΕͨΦϓγϣϯ

  6. harmony_typeof

  7. typeof null == ‘null’ ʹͳΔΑ͏ʹ͢Δౕ

  8. ΋ͱ͔Βඇਪ঑

  9. ΋ͱ͔Βඇਪ঑ UZQFPGOVMMͷ࢓༷͸ࠓ·Ͱ௨ ΓʹͳΔ༧ఆ͔ͩΒIBSNPOZ ͷౕ͸࢖Θͳ͍΄͏͕͍͍ͧ

  10. Brendan Eichᐌ͘ classͷํ͕Ձ஋͕͋Δ͠ɺopt-inͷtypeof nullΛ”null”ʹ͢Δͷ͸খ͞ͳΞΠσΞͩɻ

  11. typeof null͸͜Ε͔Β΋ͣͬͱ “object”ͳͷͰ
 ҆৺͢ΔΑ͏ʹɻɻɻ

  12. v0.12Ͱ௥Ճ͞ΕͨΦϓγϣϯ

  13. harmony_arrow_functions

  14. Ξϩʔؔ਺ => Λ࢖͑ΔΑ͏ ʹ͢ΔΦϓγϣϯ

  15. harmony_arrow_function // before var b = [1,2,3,4,5,6].map(function(x){ return x *

    x }) // after var a = [1,2,3,4,5,6].map(x => x * x)
  16. harmony_arrow_function // before var b = [1,2,3,4,5,6].map(function(x){ return x *

    x }) // after var a = [1,2,3,4,5,6].map(x => x * x) ؆қతʂʂ
  17. v0.12ͰσϑΥϧτʹͳͬͨػೳ

  18. Symbol

  19. ΦϒδΣΫτͷkeyʹར༻Ͱ͖Δ
 ಛघͳΦϒδΣΫτprivateͳ໊લͰΩʔ Λొ࿥ͨ͠Γɺিಥճආ͢Δͷʹ࢖͑Δɻ

  20. Symbol // ObjectΛ࡞Δ var obj = { name : “yosuke

    furukawa”, _name : “kaicho” // private fieldͬΆ͍ͨ͘͠ɺ֎͔ΒࢀরͰ͖ͪΌ͏ };
  21. Symbol // ObjectΛ࡞Δ var obj = { name : “yosuke

    furukawa”, _name : “kaicho” // private fieldͬΆ͍ͨ͘͠ }; // SymbolΛ࢖ͬͯ࡞Δ var _name = Symbol(“_name”) var obj = { name : “yosuke furukawa”, }; obj[_name] = “kaicho”
  22. Symbol // ObjectΛ࡞Δ var obj = { name : “yosuke

    furukawa”, _name : “kaicho” // private fieldͬΆ͍ͨ͘͠ }; // SymbolΛ࢖ͬͯ࡞Δ var _name = Symbol(“_name”) var obj = { name : “yosuke furukawa”, }; obj[_name] = “kaicho” @OBNFͷ໊લۭؒ֎͔Β@OBNFΠϯελϯεΛ ࢖͑ͳ͍ͨΊɺQSJWBUFͬΆ͘Ͱ͖Δ DPOTPMFMPHͰ΋ݟ͑ͳ͍
  23. Collections

  24. Map/Setͱ͍ͬͨίϨΫγϣϯ
 (WeakMap/WeakSetؚΉ)

  25. Collection // MapΛ࡞Δ var map = new Map(); map.set(‘yosuke’, 'JavaScript')

    map.get(‘yosuke’) // ‘JavaScript’ map.keys() // keyͷIterator for (var k of map.keys()) {console.log(k)} map.values() // valueͷIterator for (var v of map.values()) {console.log(v)} map.has(‘yosuke’) // true // forEach΋Ͱ͖Δ map.forEach((k, v) => console.log(k,v))
  26. Collection // SetΛ࡞Δ var set = new Set(); set.add(“yosuke”) set.add(“furukawa”)

    set.forEach(x => console.log(x)) // ࣮͸keysͱvaluesϝιου΋͋Δ for (var k of set.keys()) {console.log(k)} for (var v of set.values()) {console.log(v)}
  27. Collection // SetΛ࡞Δ var set = new Set(); set.add(“yosuke”) set.add(“furukawa”)

    set.forEach(x => console.log(x)) // ࣮͸keysͱvaluesϝιου΋͋Δ for (var k of set.keys()) {console.log(k)} for (var v of set.values()) {console.log(v)} 4FU .BQ͕࢖͑ΔΑ͏ʹͳͬͨɻ
  28. for-of

  29. Iterableͳ΋ͷΛ܁ΓฦͤΔ for-ofจ͕࢖͑ΔΑ͏ʹͳͬͨ

  30. for-of var res = []; for (var element of [1,

    2, 3]) { res.push(element * element); } console.log(res); // [1, 4, 9]
  31. Symbol.Iterator // 1000·Ͱͷ஋Λฦ͢fibonacciΛ࡞Δ var fibonacci = { // Symbol.iteratorΛ࣋ͭϝιουΛ࣋ͭΦϒδΣΫτʹ͢Δ [Symbol.iterator]()

    { let pre = 0, cur = 1; // iteratorΦϒδΣΫτ͸ nextϝιουΛ࣋ͭΦϒδΣΫτΛฦ͢ return { next() { [pre, cur] = [cur, pre + cur]; if (pre < 1000) return { done: false, value: pre }; return { done: true }; } } } } for (var n of fibonacci) { console.log(n); }
  32. Symbol.Iterator // 1000·Ͱͷ஋Λฦ͢fibonacciΛ࡞Δ var fibonacci = { // Symbol.iteratorΛ࣋ͭϝιουΛ࣋ͭΦϒδΣΫτʹ͢Δ [Symbol.iterator]()

    { let pre = 0, cur = 1; // iteratorΦϒδΣΫτ͸ nextϝιουΛ࣋ͭΦϒδΣΫτΛฦ͢ return { next() { [pre, cur] = [cur, pre + cur]; if (pre < 1000) return { done: false, value: pre }; return { done: true }; } } } } for (var n of fibonacci) { console.log(n); } *UFSBUPSͱ૊Έ߹ΘͤΔ͜ͱͰGPSPGͰճͤΔΑ ͏ʹͳΔ
  33. generator for-of // 1000·Ͱͷ஋Λฦ͢fibonacciΛ࡞Δ function* fibonacci(){ let pre = 0,

    cur = 1; while (pre < 1000) { // ͜͜ͰdestructuringͰ஋Λswapͤ͞Δɻ [pre, cur] = [cur, pre + cur]; // yieldͰ஋Λฦ͢ yield pre; } } for (let n of fibonacci()) { console.log(n); }
  34. generator for-of // 1000·Ͱͷ஋Λฦ͢fibonacciΛ࡞Δ function* fibonacci(){ let pre = 0,

    cur = 1; while (pre < 1000) { // ͜͜ͰdestructuringͰ஋Λswapͤ͞Δɻ [pre, cur] = [cur, pre + cur]; // yieldͰ஋Λฦ͢ yield pre; } } for (let n of fibonacci()) { console.log(n); } HFOFSBUPSͱGPSPGΛ૊Έ߹ΘͤΔ͜ͱͰΑΓ؆ ܿʹJUFSBCMFͳ΋ͷΛ࡞Δ͜ͱ͕Մೳ
  35. Promise

  36. ඇಉظؔ਺ΛPromiseͰϥοϓ ͯ͠ݺͼ΍͘͢ͳͬͨ

  37. Promise function timeout(ms) { // Promiseͷresolveؔ਺Λड͚औΔ return new Promise((onFulfilled, onRejected)

    => { // 50%ͷ֬཰ͰonFulfilled, onRejected͕ݺ͹ΕΔ setTimeout(() => Math.random() > 0.5 ? onFulfilled() : onRejected(), ms); }); } function log() { console.log('done'); } function error() { console.log('error'); } // onFulfilled͕ग़ͨΒdoneɺonRejectedͩͬͨΒerrorͱදࣔ͢Δ timeout(100).then(log).catch(error)
  38. Promise function timeout(ms) { // Promiseͷresolveؔ਺Λड͚औΔ return new Promise((onFulfilled, onRejected)

    => { // 50%ͷ֬཰ͰonFulfilled, onRejected͕ݺ͹ΕΔ setTimeout(() => Math.random() > 0.5 ? onFulfilled() : onRejected(), ms); }); } function log() { console.log('done'); } function error() { console.log('error'); } // onFulfilled͕ग़ͨΒdoneɺonRejectedͩͬͨΒerrorͱදࣔ͢Δ timeout(100).then(log).catch(error) 1SPNJTFΛ࢖͏͜ͱͰඇಉظॲཧͷهड़खஈ͕ ૿͍͑ͯΔɻ
  39. Object.Observe (ES7)

  40. Object.observeͰΦϒδΣΫ τͷ؂ࢹΛߦ͑Δ

  41. Object.observe var todos = ["eat","code","code","sleep"]; // Using Array.observe Array.observe(todos,function(changes) {

    console.log(changes); }) todos.pop() // sleep // ؂ࢹ͍ͯ͠ΔΦϒδΣΫτͷԿ͕ߋ৽͞Εͨͷ͔͕෼͔Δ // [ { type: 'splice', // object: [ 'eat', 'code', 'code' ], // index: 3, // removed: [ 'sleep' ], // addedCount: 0 } ]
  42. Object.observe var todos = ["eat","code","code","sleep"]; // Using Array.observe Array.observe(todos,function(changes) {

    console.log(changes); }) todos.pop() // sleep // ؂ࢹ͍ͯ͠ΔΦϒδΣΫτͷԿ͕ߋ৽͞Εͨͷ͔͕෼͔Δ // [ { type: 'splice', // object: [ 'eat', 'code', 'code' ], // index: 3, // removed: [ 'sleep' ], // addedCount: 0 } ] ΦϒδΣΫτͷߋ৽ݕ஌͕؆୯ʹͰ͖Δʂʂ
  43. ͋ͱ͸·ͩඇσϑΥϧτͷ··

  44. ·ͱΊΔͱ

  45. harmony_typeof͕ফ͑ͨ

  46. harmony_arrow_functions͕௥Ճ

  47. Symbol Collections for-of Promise Object.observe

  48. Symbol Collections for-of Promise Object.observe σϑΥϧτͰ࢖͑ΔΑ͏ʹͳͬͨʂ

  49. class let generator String template … ଴ͪ๬·Ε͍ͯΔػೳ

  50. class let generator String template … ଴ͪ๬·Ε͍ͯΔػೳ WҎ߱ʹظ଴ʂʂ