프로토타입이란 자바스크립트 객체가 다른 객체로부터 속성과 메소드를 상속받는 구조를 의미한다.
객체에서 속성을 못 찾으면 prototype을 따라 부모까지 찾아 올라간다.
const user = {name : '지구용사'};
console.log(user);
__proto__는 객체의 부모를 가리킨다.
function User(name) {
this.name = name;
}
//프로토타입에 메소드 추가
User.prototype.introduce = function () {
console.log("Hi😎");
}
//인스턴스 생성
const me = new User('지구용사');
//me에 introduce는 없지만 User의 prototype에서 찾아옴
me.introduce(); //Hi😎
//프로토타입 확인
console.log(Object.getPrototypeOf(me)); //{ introduce: [Function (anonymous)] }
console.log(Object.getPrototypeOf(me) === User.prototype); //true
'💻 Front > Javascript' 카테고리의 다른 글
비동기 Promise, async, await (0) | 2025.06.20 |
---|---|
비동기 setTimeout (0) | 2025.06.20 |
this 정적 바인딩 (0) | 2025.06.20 |
this 동적바인딩 (3) | 2025.06.20 |
Object (0) | 2025.06.19 |