관련있는 변수나 함수를 묶어 놓은 것.
class Person {
constructor(name, age){
// this = {};
this.name = name;
this.age = age;
// return this;
}
}
speak(){
console.log(`${this.name}: hello!`};
}
}
const naeun = new Person('naeun', 20);
console.log(naeun.name); // naeun
console.log(naeun.age); // 20
naeun.speak(); // naeun: hello!
사람의 나이가 -1로 나오는 등의 오류를 방지하기 위해 사용한다.
get age() {
return this._age;
}
set age() {
this._age = value < 0 ? 0 : value;
class Shape{
constructor(width, height, color){
this.width = width;
this.height = height;
this.color = color;
}
draw(){
console.log(`drawing ${this.color} color !`);
}
getArea(){
return width * this.height;
}
class Rectangle extends Shape {}
// Overiding
class Triangle extends Shape {
getArea(){
return (this.width = this.height) / 2;
}
}
const rectangle = new Rectangle(20, 20, 'blue');
rectangle.draw(); // drawing blue color !
console.log(rectangle.getArea()); // 400
const Triangle = new Triangle(20, 20, 'red');
Triangle.draw(); // drawing blue color !
console.log(Triangle.getArea()); // 200
Object = { key : value }
const obj1 = {}
const obj2 = new Object()
.value
vs ['value']
.value
['value']
for (key in naeun) {
console.log(key);
}
for (value of array){
console.log(value);
}
object를 clon하는 방법