js-es6
ECMAScript 6構文
Arrow
functionを=>と表現できるようになりました。
1 2 3 4 5 6 7 8 9 10 11 12 13
| es5 var add= function(a,b){ return a + b; }
es6 var add = (a,b) => { return a + b; }
var add = (a,b) => a + b;
var square = n = > n + n;
|
1 2 3 4 5 6 7 8
| var john={ name;"john", helloLater=function(){ setTimeout(()=>{ console.log(this.name); },1000); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| es5 function Person(name) { this.name=name; }
Person.prototype.greet=function(){ console.log(this.name); }
Person.crate=function(name){ return new Person(name); }
es6 class Person { constructor(name) { this.name=name; }
greet(){ console.log(this.name); }
static create(name){ return new Person(name); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| class Author extends Person {
constructor(name,book) { super(name); this.book=book; }
greet=(){ super.greet(); console.log(this.book); }
static create(){ return new Author(name,book) } }
|
Block Scoped Binding
1 2 3 4 5 6 7 8 9 10 11 12 13
| function blockScope() {
let a = "hello";
if (true) { let a = "bye"; console.log(a); }
console.log(a); }
|
Computed Property Names
1 2 3 4 5 6 7 8
| var x = 0; var obj = { ["prop_" + x]: 'a', ["prop_" + (x + 1)]: 'b', ["prop_" + (x + 2)]: 'c' };
console.log(obj);
|
Computed Property Names
1 2 3 4 5 6 7
| function increment(x, y = 1) { return x += y; }
console.log(increment(10,5)); console.log(increment(20));
|
Comments