
super()Javascript2024. 9. 4. 05:03
Table of Contents
728x90
반응형
- Constructor
constructor(생성자)를 사용하면 인스턴스화된 객체에서 다른 메소드를 호출하기 전에수행해야 하는 사용자 지정 초기화를 제공할 수 있다.
Class를 new를 붙여서 (new User("John")) 인스턴스 객체로 생성하면넘겨받은 인수와 함께 constructor가 먼저 실행된다.이때 넘겨받은 인수인 John이 this.name에 할당된다.
class User {
constructor(name) {
// 인스턴스 객체를 생성하면 constructor이 먼저 호출된다.
this.name = name;
}
sayHi() {
alert(this.name);
}
}
let user = new User("John");
user.sayHi();
- 자바스크립트에서 super
super 키워드는 자식 Class 내에서 부모 Class와 생성자를 호출할 때 사용된다.
super 키워드는 자식 Class 내에서 부모 Class의 메소드를 호출할 때 사용된다.
=> super([arguments]); // 부모 생성자 호출
=> super.functionOnParent([arguments]);
728x90
반응형
'Javascript' 카테고리의 다른 글
Sub Class(Inheritance) (0) | 2024.09.04 |
---|---|
ES6 Classes (0) | 2024.09.04 |
javascript prototype (0) | 2024.09.04 |
OOP(Object-oriented-programming) (0) | 2024.09.03 |
Event 종류 (0) | 2024.09.03 |
@min' :: 개발을 하자
github : https://github.com/dnjfht
포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!