본문으로 건너뛰기
DUChae
글 목록
1분 읽기 프론트엔드

[JS] 상속

객체는 연관된 로직들로 이루어진 작은 프로그램이라고 할 수 있다. 상속은 객체의 로직을 그대로 물려 받는 또 다른 객체를 만들 수 있는 기능을 의미한다.

  • #JavaScript

이 시리즈의 다른 글 JavaScript · 20/26

  1. 1 [JS]숫자와 문자
  2. 2 [JS]특정 문자 제거하기 - 모두 제거, 대소문자 구분
  3. 3 [JS]문자열 뒤집기
  4. 4 [JS]변수
  5. 5 [JS]연산자
  6. 6 [JS]조건문
  7. 7 [JS]함수
  8. 8 [JS]배열
  9. 9 [JS]객체
  10. 10 [JS]모듈
  11. 11 [JS]Javascript와 정규표현식
  12. 12 [JS]전역변수와 지역변수
  13. 13 [JS]값으로서의 함수와 콜백
  14. 14 [JS]클로저
  15. 15 [JS]Arguments
  16. 16 [JS]함수의 호출
  17. 17 [JS]생성자와 new
  18. 18 전역 객체
  19. 19 [JS]this
  20. 20 [JS] 상속
  21. 21 [JS] Prototype
  22. 22 [JS] 표준 내장 객체의 확장
  23. 23 [JS] Object
  24. 24 [JS] 데이터 타입
  25. 25 [JS] 참조
  26. 26 [JS] Optional

텍스트## 상속(inheritance)이란?

객체는 연관된 로직들로 이루어진 작은 프로그램이라고 할 수 있다. 상속은 객체의 로직을 그대로 물려 받는 또 다른 객체를 만들 수 있는 기능을 의미한다. 단순히 물려받는 것이라면 의미가 없을 것이다. 기존의 로직을 수정하고 변경해서 파생된 새로운 객체를 만들 수 있게 해준다.

아래 코드는 이전 시간에 살펴본 코드다.

function Person(name){
    this.name = name;
    this.introduce = function(){
        return 'My name is '+this.name; 
    }   
}
var p1 = new Person('egoing');
document.write(p1.introduce()+"<br />");

결과

My name is du

위의 코드를 아래와 같이 바꿔보자.

function Person(name){
    this.name = name;
}
Person.prototype.name=null; //prototype이라는 특수한 프로퍼티
//person이라는 생성자에는 prototype이라는 프로퍼티가 있는데 어떠한 객체가 들어가있다.
//그 객체의 name에 값을 준다.
Person.prototype.introduce = function(){
    return 'My name is '+this.name; 
}
var p1 = new Person('du');
document.write(p1.introduce()+"<br />");

결과는 같다. 하지만 상속을 위한 기본적인 준비를 마쳤다. 이제 상속을 해보자.

function Person(name){
    this.name = name;
}
Person.prototype.name=null;
Person.prototype.introduce = function(){
    return 'My name is '+this.name; 
}
 
function Programmer(name){
    this.name = name;
}
Programmer.prototype = new Person();
 
var p1 = new Programmer('du'); //객체 생성 후 egoing이라는 인자 전달
document.write(p1.introduce()+"<br />");

Programmer이라는 생성자를 만들었다. 그리고 이 생성자의 prototype과 Person의 객체를 연결했더니 Programmer 객체도 메소드 introduce를 사용할 수 있게 되었다.

Programmer가 Person의 기능을 상속하고 있는 것이다. 단순히 똑같은 기능을 갖게 되는 것이라면 상속의 의의는 사라질 것이다. 부모의 기능을 계승 발전할 수 있는 것이 상속의 가치다.

function Person(name){
    this.name = name;
}
Person.prototype.name=null;
Person.prototype.introduce = function(){
    return 'My name is '+this.name; 
}
 
function Programmer(name){
    this.name = name;
}
Programmer.prototype = new Person();
Programmer.prototype.coding = function(){
    return "hello world";
}
//Programmer는 coding과 introduce의 기능
 
function Designer(name){
	this.name= name;
}
Designer.prototype=new Person();
Designer.prototype.design=function(){
	return "beautiful!";
}


var p1 = new Programmer('du');
document.write(p1.introduce()+"<br />");
document.write(p1.coding()+"<br />");

var p2= new Designer('vista');
document.write(p2.intorudce()+"<br />");
document.write(p2.design()+"<br />");

결과

My name is du
hello world
My name is vista
beautiful!

Programmer는 Person의 기능을 가지고 있으면서 Person이 가지고 있지 않은 기능인 메소드 coding을 가지고 있다.


이 글은 원래 Velog에 2024-07-08에 올린 글입니다. 원문: [JS] 상속

마지막 수정: