LoginSignup
3
4

More than 5 years have passed since last update.

ES6の小技集

Last updated at Posted at 2016-01-16

基本 function 横にはインデント if もインデント

function () { }

if () { }

基本シングルクオテーション

'abc'

文字列連携はバッククオテーション

test_#{chatRoomId}

関数の定義

var b = function () {}

var bar = () => {
console.log("ok");
};

引数1個の時は以下でも可能

var bar = x => x+1;

class


class Person{
    //コンストラクタ
    constructor(name) {
        this.name = name;
    }
    //メソッド
    say() {
        return 'My Name is ' + this.name+'.';
    }
}
var person = new Person("Bob");
console.log(person.say());

map


[1,2,3].map(x => x * x);

構造化代入

// ES5)
function getFullName(user) {
  var firstName = user.firstName;
  var lastName = user.lastName;
  return firstName + ' ' + lastName;
}

// ES6)
function getFullName({firstName, lastName}) {
  return `${firstName} ${lastName}`;
}

// 呼び出し側
console.log(getFullName({
  firstName: 'taro',
  lastName: 'yamada',
})); // => taro yamada

末尾にカンマつけるか否か

// ES5)
var name = {
  firstName: 'taro',
  lastName: 'yamada'
}

// ES6)
const name = {
  firstName: 'taro',
  lastName: 'yamada',
}
3
4
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
3
4