컴퓨터언어/Vanilla JS
Context, Instantiation
bbanpro
2020. 5. 21. 11:16
728x90
반응형
Context : 문학에서는 "문맥"이라면, 프로그래밍에서는 현재 이 오브젝트가 document상 어느 위치에 있는지 알려준다. this 키워드 사용.
만약 어떤 함수가 {} 객체 내 속성으로 정의되어 있고 그 함수 내에서 this를 사용한다면, 해당 this는 객체 내 함수만을 가리킨다.
const obj = {
myFunc: function () {
console.log(this);
}
};
Instantiation : 클래스(Constructor Function)를 정의하고 해당 속성과 메서드를 가지는 객체들을 인스턴스화하는 것. new 키워드 사용.
클래스는 extends로 상속이 가능하며, 상속시에는 반드시 super(인자들) 메서드를 먼저 사용한 후에 오버라이딩 또는 커스터마이징해야 한다.
class Family {
constructor(position, name) {
this.position = position;
this.name = name;
};
introduce() {
console.log(`Hi, I'm ${name}! And I'm ${this.position} in my family.`);
};
};
class Child extends Family {
constructor(position, name) {
super(position, name);
console.log(this);
};
study() {
console.log("Hi, I'm studying for SAT!");
};
};
const son1 = new Child("Son1", "John");
728x90
반응형