ããã«ã¡ã¯ä¼å¡äºæ¥é¨ã®ä¸¸å±±@h13i32maruã§ãã
æè¿ã®Webããã³ãã¨ã³ãã®å¤åã¯é常ã«æ¿ãããã¡ãã£ã¨ç®ãé¢ããéã«ã©ãã©ãæ°ãããã®ãåºã¦ãã¾ãããããããªæ¿ããå¤åã®ä¸ã¤ã¨ãã¦ES6ã¨ãã次æJavaScriptã®ä»æ§ãããã¾ãããã®ES6ã¯ç¾å¨çå®ä¸ã§ãå·çæç¹ã§ã¯Draft Rev31ãå ¬éããã¦ãã¾ãã
JavaScriptã¯ECMAScript(ECMA262)ã¨ããä»æ§ããã¨ã«å®è£ ããã¦ãã¾ãã ç¾å¨ã®ã¢ãã³ãªWebãã©ã¦ã¶ã¯ECMAScript 5.1th Editionããã¨ã«ããJavaScriptå®è¡ã¨ã³ã¸ã³ãæè¼ãã¦ãã¾ãã ããã¦æ¬¡ã®ãã¼ã¸ã§ã³ã§ããECMAScript 6th Editionãç¾å¨çå®ä¸ã§ãç¥ç§°ã¨ãã¦ES6ã¨ããååããã使ããã¾ãã
ä»åã¯ãä»ã®è¨èªã«ã¯ãã£ã¦JavaScriptã«ã欲ãããªã¨æã£ã¦ããæ©è½ããJavaScriptã§ããé »åºãããã¿ã¼ã³ãçµ±ä¸çã«æ¸ããæ©è½ãä¸å¿ã«ç´¹ä»ãã¦ããã¾ãã
Class
JavaScriptã¯ããããã¿ã¤ããã¼ã¹ã®OOPãã¨å¼ã°ãã¦ããéããJavaãRubyãªã©ã®ãã¯ã©ã¹ãã¼ã¹ã®OOPãã¨ã¯å°ãæ¯è²ãéãã¾ããããããããã¿ã¤ããã¼ã¹ã®æ©è½ãå¹æçã«ä½¿ãã¨ãããã¨ã¯ããã¾ã§å°ãªãã£ãããã«æãã¾ãããããæ¬ä¼¼çãªã¯ã©ã¹æ©è½ãå®è£
ããããã¯ã©ã¹ãå®ç¾ããããã®ã©ã¤ãã©ãªã使ã£ã¦ããã°ã©ã ãæ¸ããã¨ãå¤ãã¯ãã§ããããã¯npmã§class
ã§æ¤ç´¢ããã¨ããããã®ããã±ã¼ã¸ãããããããã¨ããããããã¨æãã¾ããããã§ES6ã§ã¯ã¯ã©ã¹æ©è½ãå°å
¥ãããã¯ã©ã¹ãç°¡åã«æ±ããããã«ãªãã¾ããã
// ES5 'use strict'; function User(name){ this._name = name; } User.prototype = Object.create(null, { constructor: { value: User }, say: { value: function() { return 'My name is ' + this._name; } } }); function Admin(name) { User.apply(this, arguments); } Admin.prototype = Object.create(User.prototype, { constructor: { value: Admin }, say: { value: function() { var superClassPrototype = Object.getPrototypeOf(this.constructor.prototype); return '[Administrator] ' + superClassPrototype.say.call(this); } } }); var user = new User('Alice'); console.log(user.say()); // My name is Alice var admin = new Admin('Bob'); console.log(admin.say()); // [Administrator] My name is Bob
// ES6 'use strict'; class User { constructor(name) { this._name = name; } say() { return 'My name is ' + this._name; } } class Admin extends User { say() { return '[Administrator] ' + super.say(); } } var user = new User('Alice'); console.log(user.say()); // My name is Alice var admin = new Admin('Bob'); console.log(admin.say()); // [Administrator] My name is Bob
Function Arguments
JavaScriptã§é¢æ°ã®ããã©ã«ãå¼æ°ãå¯å¤é·å¼æ°ã使ãããã¨æã£ã¦ãè¨èªã«ã¯ç´æ¥çãªæ¹æ³ããªãã£ãããã||
ã使ã£ããã¾ããªãçãªæ¹æ³ãarguments
ã使ã£ãã¡ã¿ããã°ã©ãã³ã°çãªæ¹æ³ãåã£ã¦ãã¾ãããããã§ES6ã§ã¯é¢æ°ã®ä»®å¼æ°ã®å®£è¨æ¹æ³ãå¼·åãããèªç¶ã«æ¸ããã¨ãã§ããããã«ãªãã¾ãããããã¯å¾ã§ããã°ã©ã ãèªãã¨ãã«ãã·ã°ããã£ã ããè¦ãã°ãã®é¢æ°ãæå¾
ããå¼æ°ãããç¨åº¦ãããããã«ãªãã¨ããå¹æãããã¾ãã
// ES5 'use strict'; function loop(func, count) { count = count || 3; for (var i = 0; i < count; i++) { func(); } } function sum() { var result = 0; for (var i = 0; i < arguments.length; i++) { result += arguments[i]; } return result; } loop(function(){ console.log('hello')}); // hello hello hello console.log(sum(1, 2, 3, 4)); // 10
// ES6 'use strict'; function loop(func, count = 3) { for (var i = 0; i < count; i++) { func(); } } function sum(...numbers) { return numbers.reduce(function(a, b) { return a + b; }); } loop(function(){ console.log('hello')}); // hello hello hello console.log(sum(1, 2, 3, 4)); // 10
å®ã¯ãã®ããã©ã«ãå¼æ°ãå¯å¤é·å¼æ°ã¯é¢æ°ã®ä»®å¼æ°é¨ã ãã§ä½¿ããã¨ããããã§ã¯ãªããå¤æ°ã®ä»£å ¥å¦çå ¨è¬ãå¼·åããããã¡ã®ä¸é¨åã«ãªãã¾ããES6ã§ã®å¤æ°ã®ä»£å ¥å¦çã«ã¤ãã¦ã¯Destructuring and parameter handling in ECMAScript 6ã«ã¦ãµã³ãã«ä»ãã§æ§ã ãªãã¿ã¼ã³ãç´¹ä»ããã¦ãã¾ãã
Arrow Function
JavaScriptã§ã¯ã¤ãã³ãé§åã®å¦çãããæ¸ãã¾ããä¾ãã°DOMãã¯ãªãã¯ããããä½ãå¦çãããXHRã®ãªã¯ã¨ã¹ããå®äºãããä½ãå¦çãããå ´åãªã©ã§ãããã®ãããªå¦çãJavaScriptã§å®è£
ããã«ã¯ãã³ã¼ã«ããã¯é¢æ°ãã¤ãã³ããªã¹ãã¨å¼ã°ãããã®ã対象ã®ãªãã¸ã§ã¯ã(DOMãXHR)ã«è¨å®ãã¾ãããã®ã³ã¼ã«ããã¯é¢æ°ãç»é²ããæç¹ã§ã®this
ã«ã³ã¼ã«ããã¯é¢æ°å
ããã¢ã¯ã»ã¹ããããªãå ´é¢ãããããã¾ãããããã¾ã§ã¯ã¯ãã¼ã¸ã£ã使ã£ã¦this
ãä¿åãã¦ãããããFunction.prototype.bind
ã使ã£ã¦this
ãæç¸ããããã¦ãã¾ãããES6ã§ã¯Arrow Functionã¨å¼ã°ããæ°ããªé¢æ°å®ç¾©|å¼ãå°å
¥ããããã®this
ã«å¯¾ããç
©ãããã解æ¶ãã¦ãã¾ãã
// ES5 'use strict'; var ClickCounter = { _count: 0, start: function(selector) { var node = document.querySelector(selector); node.addEventListener('click', function(evt){ this._count++; }.bind(this)); } }; ClickCounter.start('body');
// ES6 'use strict'; var ClickCounter = { _count: 0, start: function(selector) { var node = document.querySelector(selector); node.addEventListener('click', (evt)=>{ this._count++; }); } }; ClickCounter.start('body');
Promise
ããã¾ã§XHRãªã©ã®éåæå¦çã¯éå§æã«ã³ã¼ã«ããã¯é¢æ°ãè¨å®ãã¦ãéåæå¦çãçµãã£ãããã®ã³ã¼ã«ããã¯é¢æ°ãå¼ã³åºãããã¨ããã®ãä¸è¬çã§ãããæ§ã
ãªã³ã¼ã«ããã¯é¢æ°ã®è¨å®æ¹æ³ãããã¾ãããä¾ãã°éåæå¦çã®é¢æ°ã«ã³ã¼ã«ããã¯é¢æ°ãå¼æ°ã¨ãã¦æ¸¡ããã(setTimeout
ãsetInterval
)ãéåæå¦çãè¡ããªãã¸ã§ã¯ãã«ã³ã¼ã«ããã¯é¢æ°ãç»é²ããã(XHR
ãWebWorker
)ãéåæå¦çã®æ»ãå¤ã«ã³ã¼ã«ããã¯é¢æ°ãç»é²ããã(IndexedDB
)ãªã©ãããã¾ãã
ãã®ããã«æ§ã
ãªæ¹æ³ãããããã使ãå´ã¨ãã¦ã¯ããããã®æ¹æ³ã使ãåããå¿
è¦ãããã¾ããããã§ES6ã§ã¯Promise
ã¨ããéåæå¦çãçµ±ä¸çã«æ±ãæ¹æ³ãè¨èªã¨ãã¦æä¾ãããããã«ãªãã¾ããã使ãæ¹ã¯ãéåæå¦çãè¡ãé¢æ°ã¯Promiseãæ»ãå¤ã¨ãã¦è¿ããå¼ã³åºãå´ã¯Promiseã«ã³ã¼ã«ããã¯é¢æ°ãç»é²ããã¨ãããã®ã§ãã
// ES5 'use strict'; function sleep(callback, msec) { setTimeout(callback, msec); } sleep(function(){ console.log('wake!') }, 1000);
// ES6 'use strict'; function sleep(msec) { return new Promise(function(resolve, reject){ setTimeout(resolve, msec); }); } sleep(1000).then(function(){ console.log('wake!'); });
ã¾ãéåæå¦çã§ã¯ä¾å¤å¦çãåé¡ã«ãªãã¾ããåç´ã«try-catchã§å²ã£ã¦ãéåæã§ä¾å¤ãèµ·ããã¨è£è¶³ã§ãã¾ãããããã§Promiseã§ã¯éåæå¦çã®ä¾å¤å¦çãçµ±ä¸çã«è¡ããæ¹æ³ãæä¾ãã¦ãã¾ãããã®Promiseã«ã¤ãã¦ã¯Webä¸ã§ç¡æã§èªããJavaScript Promiseã®æ¬ã大å¤åèã«ãªãã¾ãã
Generator
æå¾ã«Generatorã«ã¤ãã¦ç´¹ä»ãã¾ããããã¾ã§ã¯ãã§ã«JavaScriptã«åå¨ãããã©ä½¿ãã«ããã£ãããçµ±ä¸ããã¦ããªãã£ããã®ãæ¹åããã¨ããæ©è½ã§ãããããã®Generatorã¨ããã®ã¯å ¨ãæ°ããæ¦å¿µã¨ãã¦ES6ã«åãè¾¼ã¾ãã¦ãã¾ã*1ã
Generatorã¨ããã®ã¯é¢æ°å¦çå ã®ä»»æã®å ´æã§å¦çãä¸æ/åéã§ããä»çµã¿ãæä¾ãããã®ã§ãããã®ä»çµã¯ä¸è¬çã«ã³ã«ã¼ãã³(co-rutine)ã¨å¼ã°ãã¦ãã¾ããã³ã«ã¼ãã³ã使ãã¨ç¡éãªã¹ããã¤ãã¬ã¼ã¿ãªã©ãå®è£ ãããã¨ãã§ãã¾ãã
ãã®Generatorã¨Promiseãçµã¿åããããã¨ã§éåæå¦çãåæå¦çã®ããã«ç´åã«æ¸ããã¨ãã§ããããã«ãªãã¾ããåºæ¬çãªèãæ¹ã¯ãéåæå¦çãéå§ããããå¦çãä¸æããéåæå¦çãå®äºãããå¦çãåéãå¾ç¶ã®å¦çãå®è¡ãã¦ããã¨ãããã®ã§ããå ã»ã©ã®Promiseã®é ã§ç´¹ä»ãããµã³ãã«ã³ã¼ããGeneratorã使ã£ã¦ç´åã«æ¸ãã¦ã¿ã¾ãã以ä¸ã®ãµã³ãã«ã³ã¼ãã§ã¯Generatorã¨Promiseã使ã£ã¦éåæå¦çãç´åã«æ¸ããã¨ãã§ããcoã¨ããã©ã¤ãã©ãªã使ã£ã¦ãã¾ãã
// ES6 'use strict'; co(function*(){ console.log('sleep...'); yield sleep(1000); console.log('wake!'); });
ä»åã¯co
ã使ã£ã¦è§£èª¬ãã¾ããããco
ã使ããã«éåæå¦çãç´åã«æ¸ãä»çµã¿ã¨ãã¦async/awaitã¨ããæ©è½ãES7ã«ææ¡ããã¦ãã¾ã*2ã
Generatorã«ã¤ãã¦ã¯ç§ã®ããã°ã§ES6 Generatorã使ã£ã¦async/awaitãå®è£ ããã¡ã¢ã¨ãã¦è§£èª¬ãã¦ããã®ã§èå³ã®ããæ¹ã¯å¾¡è¦§ãã ããã
ã¾ã¨ã
ä»åã¯JavaScriptã§æ¯çãæãããã¦ããæããES6ã§ã©ã®ããã«å¤ããã®ããä¸å¿ã«ç´¹ä»ãã¾ãããããã§ç´¹ä»ããå 容ã¯ES6ã®ä¸é¨ã§ãããä»ã«ãModules, Symbol, Data Structures, Proxy, Template Stringãªã©ã®æ§ã ãªæ©è½ã追å ããã¦ãã¾ããç¾æç¹ã§ã¯ES6ã§æ¸ããã³ã¼ãããã®ã¾ã¾ãã©ã¦ã¶ãnodeã§å®è¡ããã®ã¯é£ããç¶æ³ã§ãããES6ãES5ã«ãã©ã³ã¹ãã¤ã«ãããã¼ã«ã¨ãã¦traceur-compilerã6to5ãããã®ã§ãæ軽ã«è©¦ãã¦ã¿ããã¨ãã§ãã¾ããã¾ãåãã©ã¦ã¶ããã¼ã«ãES6ã®ã©ã®æ©è½ã«å¯¾å¿ãã¦ãããã¯ECMAScript compatibility tableãåèã«ãªãã¾ãã
ES6æ代ã®JavaScriptã«åãã¦ä»ããå°ããã¤è§¦ã£ã¦ã¿ãã®ã¯ãããã§ããããï¼