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

Node.js v4 の話 #tng18

Node.js v4 の話 #tng18

東京Node学園 18時限目で発表した Node.js v4 の話です

Yosuke Furukawa

October 08, 2015
Tweet

More Decks by Yosuke Furukawa

Other Decks in Programming

Transcript

  1. 

  2. ES2015 • let/const • class • Map/Set/WeakMap/ WeakSet • Generators

    • Binary/Octal literals • Symbol • Template String literals • String.prototype.repeat • @@toStringTag • Enhanced Object Literals • unicode literals • arrow functions
  3. MyEventEmitter // v0.12 ‘use strict’; var events = require(‘events’); var

    util = require(‘util’); var EventEmitter = events.EventEmitter; var MyEventEmitter = function(data) { this.data = data; }; util.inherits(MyEventEmitter, EventEmitter); MyEventEmitter.prototype.intervalCheck = function() { var originalData = this.data; var _this = this; setInterval(function (){ if (originalData !== _this.data) { _this.emit(‘change’, new Date() + ‘: ’ + _this.data); originalData = _this.data; } }, 1000); }; module.exports = MyEventEmitter; DIFDLPXOPCKFDUQFSTFD
  4. MyEventEmitter // v4.0 ‘use strict’; const events = require(‘events’); const

    EventEmitter = events.EventEmitter; class MyEventEmitter extends EventEmitter { constructor(data) { this.data = data; } intervalCheck() { let originalData = this.data; setInterval(()=>{ if (originalData !== this.data) { this.emit(‘change’, `${new Date()}: ${this.data}`); originalData = this.data; } }, 1000); } } module.exports = MyEventEmitter;
  5. MyEventEmitter // After v4.0 ‘use strict’; const events = require(‘events’);

    const EventEmitter = events.EventEmitter; class MyEventEmitter extends EventEmitter { constructor(data) { this.data = data; } intervalCheck() { let originalData = this.data; setInterval(()=>{ if (originalData !== this.data) { this.emit(‘change’, `${new Date()}: ${this.data}`); originalData = this.data; } }, 1000); } } module.exports = MyEventEmitter; SFRVJSF͔Β͸DPOTUͰఆٛ͢Δ ෆมͳ෺ͱͯ͠มߋ͞Εͳ͍Α͏ʹ͢Δ VUJMJOIFSJUTͷ୅ΘΓʹDMBTT FYUFOETΛ࢖͏ BSSPXؔ਺Λ࢖͏ UFNQMBUFTUSJOHMJUFSBMͰจࣈྻ ݁߹Λॻ͘
  6. MyEventEmitter // v0.12 ‘use strict’; var events = require(‘events’); var

    util = require(‘util’); var EventEmitter = events.EventEmitter; var MyEventEmitter = function(data) { this.data = data; }; util.inherits(MyEventEmitter, EventEmitter); MyEventEmitter.prototype.intervalCheck = function() { var originalData = this.data; var _this = this; setInterval(function (){ if (originalData !== _this.data) { _this.emit(‘change’, new Date() + ‘: ’ + _this.data); originalData = _this.data; } }, 1000); }; module.exports = MyEventEmitter; // v4.0 ‘use strict’; const events = require(‘events’); const EventEmitter = events.EventEmitter; class MyEventEmitter extends EventEmitter { constructor(data) { this.data = data; } intervalCheck() { let originalData = this.data; setInterval(()=>{ if (originalData !== this.data) { this.emit(‘change’, `${new Date()}: ${this.data}`); originalData = this.data; } }, 1000); } } module.exports = MyEventEmitter; #FGPSF "GUFS ͖ͬ͢Γॻ͚ΔΑ͏ʹͳͬͨɻ
  7. ·ͩ • default params • rest/spread (ଟ෼v5.0Ͱenable) • tail call

    opts • Proxy • Reflect • modules ((ƅ㱼ƅ)ŋ㲎. ŞƄŰŕ!!)
  8. Strong Script • ES6 ͷίʔυΛڧ੍ͤ͞Δ࢓૊Έ • “use strong” σΟϨΫςΟϒΛॻ͘ •

    node —strong_mode hoge.js Ͱىಈ • var Λ let/constʹม͑ͨΓɺdeleteެจΛ࢖͏ͱౖΒΕͨ Γ͢Δɻ • experimental feature
  9. “use strong” “use strong”; function foo() { // v8 4.2

    enabled (io.js v2.0 also) var obj1 = ‘aaa’; // Restrict var (use let or const instead) let args = arguments; // Restrict arguments (use Rest Params …args) if (obj1 == ‘aaa’) {} // Restrict == (use strict equal ===) if (obj1 === ‘aaa’); // Restrict empty if and for for (let n in [1,2,3]) // Restrict for-in (use for-of instead) delete obj1.key // Restrict delete operator (use Map/Set instead) // v8 4.5 enabled TODO: try let undefined = ‘aaa’; // Restrict undefined binding let obj3 = { foo : ‘aaa’}; obj3.bar = ‘123’; // Restrict undefined property access }
  10. ESIntl • ECMAScript ࠃࡍԽAPI • ECMA-402 / ௨শ ESIntl •

    node͸buildͷ࣌ʹ’--with-intl=full-icu — download-all’Λࢦఆ͢Δͱ৽͘͠built-inΦ ϒδΣΫτͷIntlΦϒδΣΫτ͕༗ޮʹͳΔɻ
  11. API

  12. Buffer for/of var buf = new Buffer([1, 2, 3]); for

    (var b of buf) console.log(b) // 1 // 2 // 3 #V⒎FS͕JUFSBUPSʹʂʂʂ
  13. Simple Stream Constructor // v0.12 var Transform = require('stream').Transform; var

    util = require('util'); util.inherits(MyTransform, Transform); function MyTransform(opts){ Transform.call(this, opts); } MyTransform.prototype._transform = function(chunk, encoding, callback){ // ͜͜Ͱ ม׵ͯ͠ ... // push͢Δ this.push(chunk); }; MyTransform.prototype._flush = function(done){ // ࠷ޙʹԿ͔͚ͨ͠Ε͹͜͜Ͱ flush ͢Δ };
  14. Simple Stream Constructor // v4.0 var transform = new stream.Transform({

    transform: function(chunk, encoding, next) { // ͜͜Ͱ ม׵ͯ͠ ... // push͢Δ this.push(chunk); }, flush: function(done) { // ࠷ޙʹԿ͔͚ͨ͠Ε͹͜͜Ͱ flush ͢Δ } }); ؆୯ʂUISPVHIͱಉ͡ॻ͖ํʂʂ
  15. OS