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

春からはじめる新しいNode.js / start new Node.js in spring

春からはじめる新しいNode.js / start new Node.js in spring

関西Node学園 梅田キャンパス 1時限目 https://nodejs.connpass.com/event/82614/ でNode.js v10やEcmaScriptの新機能の話をしました。

Masashi Hirano

April 20, 2018
Tweet

More Decks by Masashi Hirano

Other Decks in Programming

Transcript

  1. EcmaScript • ࢓༷ࡦఆ·Ͱʹstage0ʙstage4·Ͱͷஈ֊͕ଘࡏ͢Δ • 0 Strawman - ΞΠσΞ • 1

    Proposal - ఏҊ • 2 Draft - υϥϑτ • 3 Candidate- ࢓༷ॻͱಉ͡ܗࣜ • 4 Finished - ࡦఆ׬ྃ
  2. Node 8.x • npm@5 • package-lock.json • 8.5͔Β͸ϑϥά෇͖ͰES Modules࢖͑Δ •

    import / export syntax • AWS LambdaͰ΋࠷ۙΑ͏΍͘v8͕࢖͑ΔΑ͏ʹ
  3. V8 engine • Node 9.x(V8 6.2) => Node 10.0(V8 6.6)

    • EcmaScript৽ػೳͷ௥Ճ(ޙड़) • ࣮ߦ଎౓ͷվળ • asyncͷύϑΥʔϚϯεվળ
  4. npm@6 • Node 10.0ʹ޲͚ͯখ͞ͳมߋ͸͋Δ • Node 4.xͱNode 7.xͷαϙʔτऴྃ • npm

    ci && npm tͳnpm cit͕௥Ճ • npm install͢Δͱ͖ʹdeprecateͷόʔδϣϯ ΛճආͳͲ
  5. deprecateͷόʔδϣϯΛճආ $ npm install example@~1.1.0 // 1.1.2 // 1.1.3 (deprecated)

    // 1.2.0 (latest) ͕Yͷ࠷৽͕ͩɺEFQSFDBUFʹͳͬ ͍ͯΔͨΊΠϯετʔϧ͞Εͳ͍
  6. Node.js v10ʹ௥Ճ͞ΕΔ EcmaScript৽ػೳ • Dynamic import • Async Iteration •

    Promise.prototype.finally • RegExp New Features • Intl.NumberFormat.formatToParts • Function.prototype.toString() • Optional catch binding • String.prototype.trimStart • String.prototype.trimEnd
  7. Dynamic Import (async() => { let count = 0; let

    id = setInterval(async() => { const i = Math.floor(Math.random() * 3) + 1; const module = await import(`./hello${i}.mjs`); module.hello(); count++; if (count === 10) { clearInterval(id); } }, 1000); })(); ಈతʹϞδϡʔϧΛJNQPSUՄ ೳʹ
  8. Async Iteration async function* gen() { while (true) { const

    res = await axios.get(‘https:// hogehoge.com'); const data = await res.data; yield data; } } (async() => { for await (const data of gen()) { console.log(data); } })(); 1SPNJTFͳ഑ྻΛGPSBXBJU PGͰඇಉظϧʔϓ͕Մೳʹ
  9. Promise.prototype.finally Promise.resolve(“then”) .then(val => { console.log(val); throw new Error(“catch") }).catch(err

    => { console.log(err.message) }).finally(() => { console.log(“finally") }); pOBMMZ ͕௥Ճ
  10. RegExp New Features const result = '123$456 $789 $ 000'.match(/(?<=\

    $)\d+/g); console.log(result); // ['456', '789'] ਖ਼نදݱͷޙಡΈ͕Մೳɻ ଞʹ΋ υοτ ͕վߦจࣈʹϚον͢ ΔΑ͏ʹͳΔTϑϥάͳͲ
  11. Intl.NumberFormat.formatTo Parts const num = 1000; const formatter = new

    Intl.NumberFormat('ja-JP', { style: 'currency', currency: 'JPY' }); const yen = formatter.formatToParts(num).map(({type, value}) => { switch (type) { case 'currency': return "<strong>" + value + "</ strong>"; default: return value; } }).reduce((string, part) => {return string + part}); console.log(yen); // => "<strong>¥</strong>1,000" ਺஋ͷΧελϜϑΥʔϚοτ ͕Մೳʹ
  12. Function.prototype.toString function /* a comment */ foo () {} foo.toString();

    // → 'function /* comment */ foo () {}' UP4USJOH ͷ࢓༷มߋ
  13. Optional catch binding try { const a = 1; a

    = 2; } catch { console.error("catch"); } DBUDIͷҾ਺ͳ͠Ͱ΋Մೳʹ
  14. String.prototype.trim{Start|End} const value = " has spaces "; console.log(`‘${value}'`); //

    => ‘ has spaces ‘ console.log(`‘${value.trimStart()}'`); // => ‘has spaces ‘ console.log(`trimEnd: '${value.trimEnd()}'`); // => ‘ has spaces‘ จࣈྻͷલޙͷεϖʔεΛআ ڈ
  15. EcmaScriptͷػೳ • shipping: ϦϦʔεࡁ • ϑϥάͳ͠Ͱ࣮ߦՄೳ • staged: ΄΅׬੒͕ͩɺ҆ఆ͍ͯ͠ΔͱΈͳ͞Εͳ͍ •

    --harmonyϑϥάͰ࣮ߦՄೳ • in progress: ։ൃத • --harmony-class-fieldsͷΑ͏ʹݸʑʹ༻ҙ͞Εͨ harmonyϑϥά
  16. harmony ֬ೝํ๏ $ node —v8-options | grep harmony --es_staging (enable

    test-worthyɾɾɾ --harmony (enable all completedɾɾɾ --harmony_shipping (enable allɾɾɾ --harmony_array_prototype_values (ɾɾ --harmony_function_sent (enableɾɾɾ ͲΜͳIBSNPOZϑϥά͕͋Δ͔͸ŠW PQUJPOTΛ࣮ߦ͢Ε͹֬ೝͰ͖Δ
  17. harmony ྫ $ node —harmony-bigint > typeof 123n ‘bigint’ >

    123n + 1n 124n ŠIBSNPOZϑϥάΛ͚ͭΔ͜ͱͰ#JH*OU΋ ࢖͑Δ /PEFW
  18. Special Thanks! ओ࠵ऀϝϯόʔ (@sbntaminif, @vanx2, @mdaisuke, @kamiyam,@leichtgewicht), ͘͞ΒΠϯλʔωοτ ༷, εϐʔΧʔͷํʑ(@mochiya98,

    @sota1235, @h_michael_z) and ࠓ೔དྷͯ͘Εͨօ༷!! 1ճ໨։࠵Ͱ͖ͯخ͍͠Ͱ͢ʂ͜Ε͔Β΋ؔ੢Node ֶԂΛ຤Ӭ͘ΑΖ͓͘͠ئ͍͍ͨ͠·͢