Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

harmony_typeof

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

΋ͱ͔Βඇਪ঑

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

harmony_arrow_functions

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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)

Slide 16

Slide 16 text

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) ؆қతʂʂ

Slide 17

Slide 17 text

v0.12ͰσϑΥϧτʹͳͬͨػೳ

Slide 18

Slide 18 text

Symbol

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

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Ͱ΋ݟ͑ͳ͍

Slide 23

Slide 23 text

Collections

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

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))

Slide 26

Slide 26 text

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)}

Slide 27

Slide 27 text

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͕࢖͑ΔΑ͏ʹͳͬͨɻ

Slide 28

Slide 28 text

for-of

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

for-of var res = []; for (var element of [1, 2, 3]) { res.push(element * element); } console.log(res); // [1, 4, 9]

Slide 31

Slide 31 text

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); }

Slide 32

Slide 32 text

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ͰճͤΔΑ ͏ʹͳΔ

Slide 33

Slide 33 text

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); }

Slide 34

Slide 34 text

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ͳ΋ͷΛ࡞Δ͜ͱ͕Մೳ

Slide 35

Slide 35 text

Promise

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

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)

Slide 38

Slide 38 text

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Λ࢖͏͜ͱͰඇಉظॲཧͷهड़खஈ͕ ૿͍͑ͯΔɻ

Slide 39

Slide 39 text

Object.Observe (ES7)

Slide 40

Slide 40 text

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

Slide 41

Slide 41 text

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 } ]

Slide 42

Slide 42 text

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 } ] ΦϒδΣΫτͷߋ৽ݕ஌͕؆୯ʹͰ͖Δʂʂ

Slide 43

Slide 43 text

͋ͱ͸·ͩඇσϑΥϧτͷ··

Slide 44

Slide 44 text

·ͱΊΔͱ

Slide 45

Slide 45 text

harmony_typeof͕ফ͑ͨ

Slide 46

Slide 46 text

harmony_arrow_functions͕௥Ճ

Slide 47

Slide 47 text

Symbol Collections for-of Promise Object.observe

Slide 48

Slide 48 text

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

Slide 49

Slide 49 text

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

Slide 50

Slide 50 text

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