Created
January 16, 2016 14:44
-
-
Save tuno-tky/c1001384be19cbc98208 to your computer and use it in GitHub Desktop.
ES2015 (ES6)についてのまとめ ref: http://qiita.com/tuno-tky/items/74ca595a9232bcbcd727
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let foo = [1, 2, 3]; | |
{ | |
let foo = [4, 5, 6]; | |
console.log(foo); | |
// => 4, 5, 6 | |
} | |
console.log(foo); | |
// => 1, 2, 3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
const PI = 3.14; | |
const circleArea = function (radius) { | |
return radius * radius * PI; | |
}; | |
console.log(circleArea(3)); | |
// => 28.26 | |
// ここでPI = 3.1415;のような再代入を行おうとすると、例外が発生する | |
} | |
console.log(PI); | |
// => undefined |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var name = Izmeal; | |
var age = 21; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var [name, age] = ['Koyabu', 20]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var array = [1, 2, 3] | |
function f(x, y, z) { } | |
f(...array); | |
//f(1, 2, 3)という引数での関数fの呼び出しと同義 | |
[...array, 4, 5, 6] | |
//[1, 2, 3, 4, 5, 6]となる | |
var x | |
var y | |
var z | |
var variables = [x, y, z] | |
[a, b, ...variables] = [1, 2, 3, 4, 5]; | |
// a = 1, b = 2, x = 3, y = 4, z = 5とした場合と同じ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function f(x, ...ys) { | |
console.log(x, ys); | |
} | |
f(2, 3, 5); | |
//=> 2 [ 3, 5 ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function multiply(a, b = 1) { | |
return a*b; | |
} | |
multiply(5); // 5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var name = 'Koyabu' | |
var hello = `My name is | |
${name}` | |
console.log(hello) | |
//=>My name is | |
//Koyabu |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Human(name) { | |
this.name = name; | |
} | |
Human.prototype.hello = function () { | |
console.log('My name is ' + this.name); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
obj = new Human('Izmeal') | |
obj.hello | |
//=> 'My name is Izmeal' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Human { | |
constructor(name) { | |
this.name = name; | |
} | |
hello() { | |
console.log('My name is ' + this.name); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
obj = new Human('Koyabu') | |
obj.hello | |
//=> 'My name is Koyabu' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Human { | |
constructor(name) { | |
this.name = name; | |
} | |
hello() { | |
console.log('My name is ' + this.name); | |
} | |
static num_of_hands() { | |
console.log(2) | |
} | |
} | |
Human.num_of_hands() | |
//=> 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Animal { | |
constructor(name) { | |
this.name = name; | |
} | |
speak() { | |
console.log(this.name + ' makes a noise.'); | |
} | |
} | |
class Dog extends Animal { | |
speak() { | |
console.log(this.name + ' barks.'); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 従来のfunctionを使った書き方 | |
var plus = function(x, y) { | |
return x + y; | |
}; | |
// アロー関数 | |
let plus = (x, y) => { | |
return x + y; | |
}; | |
// 単一式の場合はブラケットやreturnを省略できる | |
let plus = (x, y) => x + y; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var func = () => ({ foo: 1 });) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment