상식닷컴
로그인
가입하기
2026년 상식닷컴 선정 식당 & 카페 리스트
2025년 2026년 신상 호텔 리스트
최근에 오픈한 호텔을 찾는다면 살펴보세요
일주일 식단표 어플
자동 일주일 식단표 어플
안드로이드
아이폰
주식 & 코인 차트의 신
1000만원으로 2000만원 만들기 프로젝트
수정하기 - 자바스크립트에서 상속을 구현하는 방법은 무엇인가요?
닉네임
비밀번호
제목
내용
[이미지 업로드는 권한이 있는 사람만 가능. 하단 카톡으로 연락]
<a href='https://sangseek.com/sangseeks/자바/ko'>자바</a>스크립트에서 상속을 구현하는 방법은 여러 가지가 있으며, ES5 이전과 이후의 문법에 따라 다르게 접근할 수 있습니다. 여기서는 다양한 방법을 소개하고 각각의 장단점을 설명하겠습니다. 1. 프로토타입 기반 상속 (ES5 이전) 자바스크립트는 프로토타입 기반 언어입니다. 객체는 다른 객체를 프로토타입으로 가질 수 있으며, 이를 통해 상속을 구현할 수 있습니다. ```javascript function Parent() { this.parentProperty = 'I am a parent property'; } Parent.prototype.parentMethod = function() { console.log('This is a method from the parent'); }; function Child() { Parent.call(this); // 부모 생성자 호출 this.childProperty = 'I am a child property'; } // <a href='https://sangseek.com/sangseeks/프로토타입 체인/ko'>프로토타입 체인</a> 설정 Child.prototype = Object.create(Parent.prototype); Child.prototype.constructor = Child; Child.prototype.childMethod = function() { console.log('This is a method from the child'); }; // 사용 예 const childInstance = new Child(); console.log(childInstance.parentProperty); // 'I am a parent property' childInstance.parentMethod(); // 'This is a method from the parent' console.log(childInstance.childProperty); // 'I am a child property' childInstance.childMethod(); // 'This is a method from the child' ``` 장점: - 프로토타입 체인을 통해 메모리 효율적으로 메서드를 공유할 수 있습니다. 단점: - 코드가 복잡해질 수 있으며, <a href='https://sangseek.com/sangseeks/상속 관계/ko'>상속 관계</a>를 이해하기 어려울 수 있습니다. 2. <a href='https://sangseek.com/sangseeks/ES6/ko'>ES6</a> 클래스 문법 ES6(ECMAScript 2015)부터는 클래스 문법이 도입되어 상속을 보다 직관적으로 구현할 수 있습니다. ```javascript class Parent { constructor() { this.parentProperty = 'I am a parent property'; } parentMethod() { console.log('This is a method from the parent'); } } class Child extends Parent { constructor() { super(); // 부모 생성자 호출 this.childProperty = 'I am a child property'; } childMethod() { console.log('This is a method from the child'); } } // 사용 예 const childInstance = new Child(); console.log(childInstance.parentProperty); // 'I am a parent property' childInstance.parentMethod(); // 'This is a method from the parent' console.log(childInstance.childProperty); // 'I am a child property' childInstance.childMethod(); // 'This is a method from the child' ``` 장점: - 클래스 문법은 객체 지향 프로그래밍의 전통적인 개념을 따르므로 코드가 더 명확하고 가독성이 좋습니다. - `super` 키워드를 사용하여 부모 클래스의 생성자와 메서드를 쉽게 호출할 수 있습니다. 단점: - ES6 이전의 코드와 호환되지 않으며, 구형 브라우저에서는 지원되지 않을 수 있습니다. 3. Object.create()를 통한 상속 `Object.create()` 메서드를 사용하여 객체를 생성하고, 이를 통해 상속을 구현할 수도 있습니다. ```javascript const parent = { parentProperty: 'I am a parent property', parentMethod: function() { console.log('This is a method from the parent'); } }; const child = Object.create(parent); child.childProperty = 'I am a child property'; child.childMethod = function() { console.log('This is a method from the child'); }; // 사용 예 console.log(child.parentProperty); // 'I am a parent property' child.parentMethod(); // 'This is a method from the parent' console.log(child.childProperty); // 'I am a child property' child.childMethod(); // 'This is a method from the child' ``` 장점: - 간단하고 직관적으로 객체를 생성할 수 있습니다. - <a href='https://sangseek.com/sangseeks/상속 구조/ko'>상속 구조</a>를 명확히 할 수 있습니다. 단점: - `Object.create()`는 생성자 함수와는 다르게 인스턴스를 생성하는 방식이 아니므로, 생성자 패턴을 사용하는 경우와는 다르게 동작할 수 있습니다. 결론 자바스크립트에서 상속을 구현하는 방법은 다양하며, 각 방법은 특정 상황에 따라 장단점이 있습니다. ES6 클래스 문법은 현대적인 자바스크립트 개발에서 가장 많이 사용되며, 코드의 가독성과 유지보수성을 높이는 데 기여합니다. 그러나 기존의 프로토타입 기반 상속이나 `Object.create()`를 사용하는 방법도 여전히 유용하며, 상황에 따라 적절한 방법을 선택하는 것이 중요합니다.
이용안내
커뮤니티 이용안내
×
- 게시한 게시글로 발생하는 문제는 게시자에게 책임이 있습니다.
- 게시글이 타인/타업체의 저작권을 침해할 경우 모든 책임은 게시자에게 있습니다. 게시자가 모든 손해를 부담해야 합니다.
- 상식닷컴 운영자는 게시자와 상의하지 않고 게시글을 수정 또는 삭제할 수 있습니다.
- 상식닷컴 운영자는 깨끗한 커뮤니티 공간을 만드는 것이 1순위입니다.
수정하기
취소하기