ECMAScript6は次期javascriptの仕様で各々ブラウザーで実装状況に開きがある。
今一番実装が進んでいるのがFireFoxである。
最新の実装状況は下記リンクから参考にして下さい。
ECMAScript 6 compatibility table[http://kangax.github.io/compat-table/es6/]
ここではECMAScript6を使いたいのでtraceur-compilerを使用する。
使用法は下記リンク参照
https://github.com/google/traceur-compiler/wiki/GettingStarted
htmlファイルを作って下記のソースを追加する
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<script src="https://google.github.io/traceur-compiler/bin/traceur.js"></script>
<script src="https://google.github.io/traceur-compiler/src/bootstrap.js"></script>
<script>
traceur.options.experimental = true;
</script>
アロー記法
関数オブジェクトを短くかける、Rubyっぽい
var square_1 = function(x){
return x * x
};
var square_2 = (x) => { return x * x; };
//引数が一つの場合は括弧を省略できる
//var square_2 = x => return x * x;
console.log(square_1(10));
console.log(square_2(10));
Rubyでは以下のように似たような感じで書ける
#Rubyではprocオブジェクト
ruby_example = ->(x){ return x * x }
ruby_example.call(2)