ECMAScriptã¨ã¯?
JavaScriptã¯1995å¹´ã«Webã§å®è¡ã§ããã¹ã¯ãªããè¨èªã¨ãã¦éçºãããããã§ããããã©ã¦ã¶ãã³ãã¼ã«ããç¬èªã®æ¡å¼µãå¤ãäºææ§ãä½ãã£ãã®ã§ããã©ã¦ã¶éã§åãããåããªãã£ããããã£ãã®ã§ã¯ãªããã¨ã
ããã§ãEcmaã¤ã³ã¿ã¼ãã·ã§ãã«ãä¸å¿ã¨ãªããJavaScriptã®ä¸æ ¸ä»æ§ãæãåºãã¦æ¨æºåããã®ãECMAScriptã®ããã§ãã
ã»ECMAScript 2015(ES6)ã®æ¦è¦ã¨æ¬¡ä¸ä»£JavaScriptã®æ°ããªæ©è½ | HTML5Experts.jp
è¨èªä»æ§ãæ´çãã¦ããã¾ãããã¨ãããã¨ã¿ããã§ãã
ES2015ï¼ES6ï¼ã§è¿½å ãããä»æ§
ECMAScript 6th editionã®6ãåã£ã¦ECMAScript 6ï¼ES6ï¼ã¨å¼ã°ãã¦ãã¾ãããæ£ç¢ºãªå¼ç§°ã¯ECMAScript 2015ï¼ES2015ï¼ã¨ããããã§ãã
- letã»constãã¼ã¯ã¼ãã«ããå¤æ°å®£è¨
- classãã¼ã¯ã¼ãã«ããã¯ã©ã¹å®£è¨
- é¢æ°ã®å¼æ°ã®ããã©ã«ããã©ã¡ã¼ã¿ï¼Default Parametersï¼
- é¢æ°ã®å¯å¤é·å¼æ°ï¼Rest Parametersï¼
- ã¢ãã¼é¢æ°ï¼Arrow Functionsï¼
- ã¸ã§ãã¬ã¼ã¿é¢æ°ï¼Generator Functionsï¼
- é åå±éï¼Array Spreadï¼
- åå²ä»£å ¥ï¼Destructing Assignmentï¼
- æååã®ãã³ãã¬ã¼ããªãã©ã«ï¼Template Stringsï¼
- importã¨exportã«ããã¢ã¸ã¥ã¼ã«æ§æï¼Moduleï¼
ãªã©ãããªãæ°ããææ³ã追å ããã¦ãã¾ãã
ãã©ã¦ã¶ã®JavaScriptã¨ã³ã¸ã³ï¼JavaScript runtimeã¨ãè¨ãï¼ï¼
ãã©ã¦ã¶ãJavaScriptãå®è¡ã§ããã®ã¯ãJavaScript runtimeãECMAScriptã®æ§æã解éãã¦å®è¡ãã¦ããããã®ããã§ãã
ãã ããã®JavaScript runtimeããã©ã¦ã¶ãã¨ã«ç°ãªãããã§ãã
ãã©ã¦ã¶ | ã¬ã³ããªã³ã°ã¨ã³ã¸ã³ | JavaScript runtime |
---|---|---|
Chrome | Blink | V8 |
Opera | Blink | V8 |
Safari | Webkit | JavaScript(Nitro) |
PhantomJS | Webkit | V8 |
Firefox | Gecko | SpiderMonkey |
IE | Trident | Chakra |
ã»PhantomJSã¨åãã©ã¦ã¶ã®Javascriptã¨ã³ã¸ã³ã¾ã¨ã | tsuchikazu blogÂ
ã¤ã¾ããES6使ããããã©ES5ã¾ã§ãã対å¿ã§ãã¦ãªããã©ã¦ã¶ã§ã¯ãES6ã使ããªããã¨ã«ãªãã¾ãã
ã§ããES6使ã£ã¦éçºãããã¨ããå ´åã
å®å ¨ã§ã¯ãªãã§ãããBabelï¼æ§6to5ï¼ãªã©ã®ãã©ã³ã¹ãã¤ã©ãpolyfillã©ã¤ãã©ãªãæ´»ç¨ããæ¹æ³ãããã¾ãã
ãã©ã³ã¹ãã¤ã©
æ°ããææ³ã使ã£ãã½ã¼ã¹ã³ã¼ããES5 ã¾ã㯠ES3 ã¾ã§ã®ææ³ã«ç½®ãæããã½ã¼ã¹ã³ã¼ãå¤æãã¼ã«ã
- Babel
- buble
- Traceur Compiler
- Rollup
ãªã©ç¨®é¡ãçµæ§ããããã§ããÂ
AltJSï¼Alternative JavaScriptï¼
AltJSï¼Alternative JavaScriptï¼ã¨ã¯ããã®åã®éãJavaScriptã®ä»£æ¿è¨èªã§ããJavaScriptã¨åç以ä¸ã®æ©è½ãæä¾ãã¦ããè¨èªã¨ãããã¨ãããã§ãã
- TypeScript
- CoffeeScript
- Dart
- Haxe
- jsx
ã³ã³ãã¤ã«ããããã¨ã§JavaScriptã®ã³ã¼ãã«ãªãããã§ãã
ã»WEB+DB PRESS Vol.87ã®ECMAScript 6ç¹éãèªãã (+TypeScript) - dackdive's blog
ã»ã¬ããã¨ã³ã¸ãã¢ã®ããã®è¿ä»£Webããã³ãã¨ã³ãäºæ - Qiita
ES6éçºç°å¢ãä½ã£ã¦ã¿ã
Node.jsãã¤ã³ã¹ãã¼ã«ããã¦ããå¿ è¦ããããnpmã³ãã³ããå¿ è¦ã«ãªã£ã¦ãã¾ãã Node.jsãã¤ã³ã¹ãã¼ã«ããnpmã³ãã³ãã§å¿ è¦ãªãã®ãã¤ã³ã¹ãã¼ã«ãã¦ããæãã§ãããããÂ
Â
- Node.js
- Babel
- Polyfill
- webpack
- Mocha
- Karma
â© Â ä¸è¨ãµã¤ãã詳ããã§ããÂ
ãããªãããã
Babel6ã«ãªã£ã¦ãããããä»æ§ãå¤ãã£ãããã§ãå°å ¥ã®ä»æ¹ãå¤ãã£ãããã§ãã
ãããç¥ããã«ãããªããããã¾ããã
ã»babel 6 ãã£ã¦ã¿ã - Qiita
ã»babel6ã§ã®å¤æ´ç¹ãGulpã»Webpackã®è¨å® - Qiita
ã¾ãã¯ãã³ãã³ãããã³ãããéãã¾ããbabel-cliãã¤ã³ã¹ãã¼ã«ãã¾ããä»åã¯ç®¡çè ã§ã¯ãªããã¦ã¼ã¶ã¼ã§ã³ãã³ãããã³ãããéãã¦ã¾ãã
npm install --global babel-cli
ä¸è¨ã³ãã³ãã§babelã®ãã¼ã¸ã§ã³æ å ±ã表示ãããã°ãã¤ã³ã¹ãã¼ã«æåã§ããÂ
babel --version
npmã§nodeã®ã¢ã¸ã¥ã¼ã«ï¼ä»åã ã¨babel-cliï¼ãã°ãã¼ãã«ã«ã¤ã³ã¹ãã¼ã«ããã¨ã
C:\Users\ã¦ã¼ã¶ã¼å\AppData\Roaming\npm\node_modules
ã«node_modulesã¨ãããã©ã«ãã®ä¸ã«ã¢ã¸ã¥ã¼ã«ã追å ããã¦ããã®ã§ãç°å¢å¤æ°ã«ãã¹ã追å ãã¦ãã㨠nodeã®requireå ã®ãã©ãã«ã解æ¶ã§ããããã§ãã
ä»åã¯ãè¨å®ãã¾ããã§ãããè¨å®ãã¦ããããã¨æãã¾ãã
ã»Windowsã®node.jsã§requireæ¤ç´¢ãã¹ã®åæå¤ãèªåè¨å®ãã - Null and void
Â
次ã«ãpackage.jsonãã¡ã¤ã«ãä½æãã¾ããããã¸ã§ã¯ã㯠package.json ã§ç®¡çãããã¨ãä¸è¬çã®ãããªã®ã§ããªãå ´åã¯ä½æãã¦ããã°ééããªããã¨ã
é©å½ãªå ´æã«ãã©ã«ããä½æï¼ä»åã¯C:Â¥ES6ãä½æï¼ãã¦ããã¦ãcdã³ãã³ãã§ãã®ãã©ã«ãï¼ããã¸ã§ã¯ãï¼ã®å ´æã¾ã§ç§»åãããã
npm init
ã³ãã³ããå®è¡ãã¾ãã対話å¼ã§ ããã¸ã§ã¯ãåãã¦ã¼ã¶ã¼å, ãã¼ã¸ã§ã³çªå·, ãã®ä»è«¸ã èããã¾ãããä»åã¯Enteré£æã§ãããã§package.jsonãã¡ã¤ã«ãç¨æããã¾ãã
ãbabel-preset-es2015ããã¤ã³ã¹ãã¼ã«
Babelã«ES2015ã®ã³ã³ãã¤ã«ã®å¦çãè¡ããããbabel-preset-es2015ãã¨ããããªã»ãããããã¸ã§ã¯ãã«ã¤ã³ã¹ãã¼ã«ãã¾ãã--save-devã¯installããlibraryã®æ å ±ãèªåã§package.jsonã«æ¸ãã¦ãããoptionã®ããã§ãã
npm install babel-preset-es2015 --save-dev
.babelrcãã¡ã¤ã«ã®æºå
.babelrcã¨ããè¨å®ãã¡ã¤ã«ãç¨æããããã«Babelã§ããããå¦çãããªã»ããããã©ã°ã¤ã³ã¨ãã¦ç»é²ãã¦ããå¿ è¦ãããã¾ãã
echo '{ "presets": ["es2015"] }' > .babelrc
ãã®ãããªãæ§æã«ãªã£ã¦ããã°OKã
ãã®ãã©ã«ãï¼ããã¸ã§ã¯ãï¼ã«ãES6ã§è¨è¿°ãããã¡ã¤ã«ãä½æãä¿åãã¾ãã
"use strict"; class User { constructor(name) { this.name = name; } greet() { return 'Hello, My name is ' + this.name; } } var bob = new User("Bob"); bob.greet();
ãã¡ã¤ã«ãä½æããããES5ã«å¤æãããã¡ã¤ã«ãä¸è¨ã³ãã³ãã§ä½æãã¾ããï¼ãã©ã³ã¹ãã¤ã«å®è¡ï¼
babel test.js -o test-es5.js
test-es5.jsãã¡ã¤ã«ãä½æããã¾ãããÂ
ãã¡ã¤ã«ã®ä¸èº«ã確èªããã¨ãES5ã«å¤æããã¦ãããã§ããÂ
"use strict"; var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } var User = function () { function User(name) { _classCallCheck(this, User); this.name = name; } _createClass(User, [{ key: "greet", value: function greet() { return 'Hello, My name is ' + this.name; } }]); return User; }(); var bob = new User("Bob"); bob.greet();
Babelã§ãã©ã³ã¹ãã¤ã«æåã§ãã
Polyfillã®ã¤ã³ã¹ãã¼ã«
Babel ã§ãã©ã³ã¹ãã¤ã«ãããã¨ã§ãå¤ããã©ã¦ã¶ã§ãåä½ãã Javascript ãåºåã§ããããã«ãªã£ãã®ã§ãããå¤ããã©ã¦ã¶ã®æ¨æºãªãã¸ã§ã¯ãã®ä¸ã«ã¯ ES6 ã«å¯¾å¿ãã¦ããªããã®ãããã¾ãã ãã®ããã«æ¨æºãªãã¸ã§ã¯ããæ¡å¼µãã Polyfill ãå°å ¥ããããã§ãã
npm install babel-polyfill --save-dev
æ¨æºãªãã¸ã§ã¯ããæ¡å¼µãããã¨ã¯ãæ¢åã®ç°å¢ã¨äºææ§ã失ããã¨ã«ãªãã¾ããPolyfill ãå°å ¥ããã¨ãã¯ãæ¨æºãªãã¸ã§ã¯ããæ¡å¼µãã¦ãããã¨ãã¡ããã¨èªèããããã§ä½¿ãå¿ è¦ãããããã§ãã
webpackã®ã¤ã³ã¹ãã¼ã«
webpack 㯠WebApp ã«å¿ è¦ãªãªã½ã¼ã¹ã®ä¾åé¢ä¿ã解決ããã¢ã»ããï¼é å¸ç©ï¼ãçæãããã«ããã¼ã«ï¼è¦ããã«ã³ã³ãã¤ã©ï¼ã§ããJavaScript ã ãã§ãªããCoffeeScript ã TypeScriptãCSS ç³»ãç»åãã¡ã¤ã«ãªã©ãæ±ããã¨ãã§ãã¾ãã
ã»webpack ã§å§ããã¤ãããã®ããã³ãã¨ã³ãéçº - Qiita
ã¨ãããã¨ã§ãã¤ã³ã¹ãã¼ã«ãã¾ããwebpack ã¯æ§ã å½¢å¼ã®ãã¼ã¿ãæ±ãã®ã«ããã©ã°ã¤ã³å½¢å¼ã§å¯¾å¿ãã¦ãããbabel ã使ãå ´å㯠babel-loader ã使ãã¾ããwebpackã¯ã°ãã¼ãã«ã§ã¤ã³ã¹ãã¼ã«ãã¾ãã
npm install -g webpack
npm install babel-loader --save-dev
interpret ã«å¯¾å¿ãã¦ãããã¼ã«ï¼webpackã¯interpretããã¼ããã¦ããï¼ã«é¢ãã¦ã¯ãbabel-register ãã¤ã³ã¹ãã¼ã«ããã°ããããªã ES6 ã«ã対å¿ã§ããããã§ãã
npm install babel-register --save-dev
webpackã®è¨å®ãã¡ã¤ã«ï¼é常ã¯webpack.config.jsï¼ãããã¸ã§ã¯ãç´ä¸ã«ä½æãã¾ãã
webpack.config.babel.jsã¨ããååã«ãã¦ããã¨ãbabelãªã³ã¼ãã¨ãã¦èªãã§ãããï¼webpackã¯interpretããã¼ããã¦ããããããã®babelã«ã.babelrcã®å 容ã¯é©ç¨ãããï¼ããã§ãã
Â
const path = require("path") module.exports = { // ã©ã®ãã¡ã¤ã«ããã«ãããã®ããæå®ãè¤æ°å¯ã entry: { test: './src/test.js', main: './src/main.js', }, // åºåãããã¡ã¤ã«åã¨åºåå ãã¹ output: { path: path.join(__dirname + '/web'), filename: '[name].js' }, // ãã©ã³ã¹ãã¤ã«ãããåã®jsãã¡ã¤ã«ã¨ãã©ã³ã¹ãã¤ã«å¾ã®jsãã¡ã¤ã«ã®ã½ã¼ã¹ããã devtool: 'inline-source-map', // module module: { loaders: [{ // .js ã®ãã¡ã¤ã«ã«å¯¾ã㦠test: /\.js$/, include: [ path.join(__dirname + '/src'), path.join(__dirname + '/test'), ], exclude: /node_modules/, loader: 'babel-loader', // loader ã§ãã¼ãã®æå®ãï¼ babel-loader ã使ç¨ã㦠babel ã§ãã©ã³ã¹ãã¤ã«ããï¼ }] } }
ãã©ã«ãã®æ§æãå¤æ´ãã¾ããsrcãã©ã«ãã®ä¸ã«ES6ã§è¨è¿°ãããtest.jsãmain.jsãä½æãã¦ããã¾ããwebãã©ã«ãã«ã¯index.htmlãä½æãã
<html> <head> <meta charset="utf-8"> </head> <body> <script src="main.js"></script> <script src="test.js"></script> </body> </html>
ã¨ãã¦ããã¾ããÂ
webpackã³ãã³ãã³ãã³ããå®è¡ãã¾ãã
webpack
index.htmlããã©ã¦ã¶ã§è¡¨ç¤ºãããããããã¼ãã¼ã«ã®ãconsoleãã確èªãã¾ãã
 ä½æ ããmain.jsã®å 容ã¯èªã¿è¾¼ã¾ãã¦ã¾ãããtest.jsã®å 容ãåæ ãããªãï¼æ¶ï¼ã
Mochaã¨Karmaã«ã¤ãã¦ã¯ã¾ãã®æ©ä¼ã«ãã©ã¤ãã¦ã¿ããã¨æãã¾ãã
追è¨
test.jsã®å 容ãééã£ã¦ã¾ãããreturnããConsoleã«è¡¨ç¤ºãããªãã£ã¦ãã¨ã§ãããÂ
è¨æ£åã®test.js
"use strict"; class User { constructor(name) { this.name = name; } say() { return 'Hello, My name is ' + this.name; } } var bob = new User("Bob"); bob.say();
è¨æ£å¾ã®test.js
"use strict"; class User { constructor(name) { this.name = name; } say() { console.log('Hello, My name is ' + this.name); } } var bob = new User("Bob"); bob.say();
â© Â ãã©ã«ãæ§æãªã©ç¶æããã¾ã¾ã³ã³ãã¤ã«ããæ¹æ³ã¯ä¸è¨ãµã¤ãã¸
ã»Webpackã§è¤æ°ã®ãã¡ã¤ã«ããã®ã¾ã¾ãã³ãã«ãã - ã¯ãã¸ãæ¥è¨
Â
ä»åã¯ãã®ã¸ãã§ã
Â
Â