2019/06/09
// ES6+
class Person {
constructor(name) {
this.name = name;
}
getName() {
return this.name;
}
static isPerson(obj) {
return obj instanceof this;
}
}
const delivan = new Person('delivan');
console.log(delivan.getName()); // delivan
console.log(Person.isPerson(delivan)); // true
// ES5
function Person(name) {
this.name = name;
}
Person.prototype.getName = function() {
return this.name;
};
Person.isPerson = function(obj) {
return obj instanceof this;
};
var delivan = new Person('delivan');
console.log(delivan.getName()); // delivan
console.log(Person.isPerson(delivan)); // true
use strict
가 강제된다.Person.prototype = { ... }
이렇게 할 수 없다.)// ES6+
class Rectangle {
constructor(x, y) {
this.x = x;
this.y = y;
}
getWidth() {
return this.x;
}
getHeight() {
return this.y;
}
}
class Square extends Rectangle {
constructor(x) {
super(x, x);
}
getArea() {
return super.getWidth() * super.getWidth();
}
}
const square = new Square(2);
console.log(square.getWidth()); // 2
console.log(square.getHeight()); // 2
console.log(square.getArea()); // 4
// ES5
var Rectangle = function(x, y) {
this.x = x;
this.y = y;
};
Rectangle.prototype.getWidth = function() {
return this.x;
};
Rectangle.prototype.getHeight = function() {
return this.y;
};
var Square = function(x) {
Rectangle.call(this, x, x);
};
Square.prototype = new Rectangle();
Square.prototype.constructor = Square;
Square.prototype.getArea = function() {
return this.getWidth() * this.getHeight();
};
var square = new Square(2);
console.log(square.getWidth()); // 2
console.log(square.getHeight()); // 2
console.log(square.getArea()); // 4
// ES6+
const NAME = Symbol('이름');
const AGE = Symbol('나이');
const delivan = {
[NAME]: '유정혁',
[AGE]: 27,
};
console.log(delivan[Symbol('이름')]); // undefined
for (prop in delivan) {
console.log(delivan[prop]); // 출력 X
}
Object.keys(delivan).forEach(key => {
console.log(delivan[key]); // 출력 X
});
Object.getOwnPropertySymbols(delivan).forEach(key => {
console.log(delivan[key]); // 출력 O
});
Reflect.ownKeys(delivan).forEach(key => {
console.log(delivan[key]); // 출력 O
});
// ES5는 없음
// ES6+
const obj = (() => {
const privateVariable = Symbol('private');
const publicVariable = Symbol.for('public');
return {
[privateVariable]: '외부에서 보이지만 접근하진 못함',
[publicVariable]: "obj[Symbol.for('public')] 으로 접근 가능",
};
})();
console.log(obj);
console.log(obj[Symbol.for('public')]);
// ES5는 없음
// ES6
const zeroToFiveIterator = {
value: 0,
next() {
return {
done: this.value >= 5,
value: this.value++,
};
},
};
const zeroToFive = {
[Symbol.iterator]() {
return zeroToFiveIterator;
},
};
console.log([...zeroToFive]);
// ES5
var zeroToFiveIterator = {
value: 0,
next: function() {
return {
done: this.value >= 5,
value: this.value,
};
},
};
var zeroToFive = [];
var obj = zeroToFiveIterator.next();
for (;;) {
var obj = zeroToFiveIterator.next();
if (obj.done) {
break;
} else {
zeroToFive.push(zeroToFiveIterator.value++);
}
}
console.log(zeroToFive);
[Symbol.iterator]()
를 실행한 결과 객체를 들고 객체 내부의 next() 메소드를 done 프로퍼티가 true가 나올 때까지 반복하여 호출하는 것이다.