LoginSignup
26
28

More than 5 years have passed since last update.

ES6チートシート

Last updated at Posted at 2015-08-28

ECMA Script 2015こと、ES6のまとめ

letとconstによる変数宣言

letはブロックスコープで再代入可能。
constはvarと同じ関数スコープで再代入不可な定数宣言。

Default Parameter

関数に初期値を与える

const increment = function(a = 1) {
  return a+1;
}

テンプレート文字列

``で囲った文字列内の${と}で囲った式は実行次に計算されて展開される

let x = 1;
let s = `${x} 足す 1 は ${x + 1} です`;

Destructing Assignment(分割代入)

let [a, b, c] = [1, 2, 3, 4];
let {a, b, c} = {a:1, b:2};

代入時に足りない分はundefinedに、余った分は切り捨てられる。

spread operator

...で関数の複数の引数や配列の複数の要素が拡張する。

配列の最後を拡張

let [a, b, ...c] = [1, 2, 3, 4];
// cは[3, 4]

配列の途中を拡張

let [a, ...b, c] = [1, 2, 3, 4];
// bは[2, 3]

引数の場合

const sum = function (...args) {
  let total=0;
  for (a of args) {
    total += a
  }
  return total;

Arrow Function

() => {}で関数を記述。thisにバインドされる。

const increment = (a = 1) => { a + 1; }

※ Javaのlambdaのような->じゃなくって=>なので注意。

ジェネレータ

他の言語にあるyeildによるジェネレータ。
関数宣言にはfunctionに*がつく。


function* range(max = 10) {
  for (let i=0; i<max; i++) {
    yeild i;
  }
}

yeildの戻り値は{value, done}。valueはyeildを発生時の値。doneがtrueの時は終了。 ジェネレータオブジェクトからはnext()メソッドで次のyeildまで実行。

Promise

省略

class

他の言語によくあるclass


class Foo extends Bar {
  constructor(bazz) {
    super();
    this.bazz = bazz;
  }
  getBazz() {
    return this.bazz;
  }
  static hello() {
    return "hello";
  }
}

let foo = new Foo("hoge");
console.debug(foo.getBazz());
console.debug(Foo.hello());

classでクラス宣言。extendsで他のクラスを継承。constructorがコンストラクタ。staticでクラスメソッドを定義。

おしまい

他にも書いておいたほうがいいものはあるかな?

26
28
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
26
28