Upgrade to Pro — share decks privately, control downloads, hide ads and more …

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

Yosuke Furukawa
October 29, 2014
110k

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

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

Yosuke Furukawa

October 29, 2014
Tweet

Transcript

  1. 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) ؆қతʂʂ
  2. Symbol // ObjectΛ࡞Δ var obj = { name : “yosuke

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

    furukawa”, _name : “kaicho” // private fieldͬΆ͍ͨ͘͠ }; // SymbolΛ࢖ͬͯ࡞Δ var _name = Symbol(“_name”) var obj = { name : “yosuke furukawa”, }; obj[_name] = “kaicho”
  4. 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Ͱ΋ݟ͑ͳ͍
  5. 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))
  6. 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)}
  7. 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͕࢖͑ΔΑ͏ʹͳͬͨɻ
  8. for-of var res = []; for (var element of [1,

    2, 3]) { res.push(element * element); } console.log(res); // [1, 4, 9]
  9. 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); }
  10. 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ͰճͤΔΑ ͏ʹͳΔ
  11. 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); }
  12. 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ͳ΋ͷΛ࡞Δ͜ͱ͕Մೳ
  13. 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)
  14. 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Λ࢖͏͜ͱͰඇಉظॲཧͷهड़खஈ͕ ૿͍͑ͯΔɻ
  15. 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 } ]
  16. 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 } ] ΦϒδΣΫτͷߋ৽ݕ஌͕؆୯ʹͰ͖Δʂʂ