Skip to content

Instantly share code, notes, and snippets.

@tuno-tky
Created January 16, 2016 14:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tuno-tky/c1001384be19cbc98208 to your computer and use it in GitHub Desktop.
Save tuno-tky/c1001384be19cbc98208 to your computer and use it in GitHub Desktop.
ES2015 (ES6)についてのまとめ ref: http://qiita.com/tuno-tky/items/74ca595a9232bcbcd727
let foo = [1, 2, 3];
{
let foo = [4, 5, 6];
console.log(foo);
// => 4, 5, 6
}
console.log(foo);
// => 1, 2, 3
{
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
var name = Izmeal;
var age = 21;
var [name, age] = ['Koyabu', 20];
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とした場合と同じ
function f(x, ...ys) {
console.log(x, ys);
}
f(2, 3, 5);
//=> 2 [ 3, 5 ]
function multiply(a, b = 1) {
return a*b;
}
multiply(5); // 5
var name = 'Koyabu'
var hello = `My name is
${name}`
console.log(hello)
//=>My name is
//Koyabu
function Human(name) {
this.name = name;
}
Human.prototype.hello = function () {
console.log('My name is ' + this.name);
};
obj = new Human('Izmeal')
obj.hello
//=> 'My name is Izmeal'
class Human {
constructor(name) {
this.name = name;
}
hello() {
console.log('My name is ' + this.name);
}
}
obj = new Human('Koyabu')
obj.hello
//=> 'My name is Koyabu'
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
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.');
}
}
// 従来のfunctionを使った書き方
var plus = function(x, y) {
return x + y;
};
// アロー関数
let plus = (x, y) => {
return x + y;
};
// 単一式の場合はブラケットやreturnを省略できる
let plus = (x, y) => x + y;
var func = () => ({ foo: 1 });)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment