ES2015の今とこれから
- 35. function Hoge() {
this.name = ‘Hoge Class’;
}
Hoge.log = function() {
console.log(‘log method’);
};
Hoge.prototype.name = function() {
console.log(this.name + ‘ です’);
};
class Hoge {
constructor() {
this.name = ‘Hoge Class’;
}
static log() {
console.log(‘log method’);
}
name() {
console.log(this.name + ‘ です’);
}
}
// extendsもあるよ
class Foo extends Hoge {}
- 38. hoge = 1; // => global汚染
var foo = ‘abc’; // => ホイスト、再代入可
function hoge() {
console.log(i);
for(var i = 0; i < 10; i++) {
console.log(i);
}
}
hoge(); // => 通っちゃう
let hoge = 1; // => ホイストなし、再代入可
const foo = ‘abc’; // => ホイストなし、再代入不可
function hoge() {
console.log(i);
for(let i = 0; i < 10; i++) {
console.log(i);
}
}
hoge(); // => Error
- 44. var a = 1;
var b = 2;
var tmpB = b;
var b = a;
var a = tmpB;
var a = 1;
var b = 2;
var [b, a] = [a, b];
let [c, ...d] = [1, ‘a’, ‘b’, ‘c’];
- 47. var hoge = function(a, b) {
var args = Array.prototype.slice.call(
arguments,
hoge.length
);
};
const hoge = (a, b, ...args) => {
}
- 50. var hoge = function(name) {
var foo = name || ‘’;
console.log(foo);
}
const hoge = (name = ‘’) => {
console.log(name);
}
- 53. hoge = (arr) => {
let nextIndex = 0;
return {
next: () => {
return nextIndex < arr.length ?
{ value: arr[nextIndex++], done: false} :
{ done: true };
}
}
}
foo = hoge([1, 2]);
console.log(foo.next()); // => 1, done: false
console.log(foo.next()); // => 2, done: false
console.log(foo.next()); // => done: true
- 55. function* hoge() {
var index = 0;
while(true) yield index++;
}
var gen = hoge();
gen.next(); //=> value: 0, done: false
gen.next(); //=> value: 1, done: false
gen.return(); //=> value: undefined, done: true
- 58. var hoge = ‘ほげお’;
console.log(‘this name is ‘ + hoge);
let hoge = ‘ほげお’;
console.log(`this name is ${hoge}`);