Prototype 상속과 Super 로의 접근

황제낙엽 2012.09.18 19:30 조회 수 : 64

sitelink1  
sitelink2  
sitelink3  
sitelink4  
extra_vars4  
extra_vars5  
extra_vars6  

<Code>


var Parent = function()

{

    this.id = "parent_member_id";

};

Parent.prototype.id = "parent_global_id";

Parent.prototype.getString = function()

{

    console.debug("Parent getString...");

};


var Child = function() {};



var F = function(){};

F.prototype = Parent.prototype;

Child.prototype = new F();

Child.prototype.getString = function()

{

    Parent.prototype.getString.call(this);

    console.debug("Child getString...");

};


var cObj = new Child();


1. console.debug(cObj.id);

    output >> parent_global_id

    

    Parent 클래스내에서 this 로 선언한 id 변수는 Parent 에서만 사용할 수 있는 member 변수이다

    해당 변수에 접근하기 위해서는 Parent 를 instance화 (new Parent) 하여 접근하면 된다


2. console.debug(cObj.getString());

    output >> Parent getString...

                    Child getString...


    Child 의 instance 인 cObj 의 getString 이 실행하면 내부에서는 Parent의 getString 을 수행하게 된다

    물론 Parent 의 getString 은 prototype 영역에 정의해야만 한다

번호 제목 글쓴이 날짜 조회 수
30 중첩 함수, 함수 클로저 황제낙엽 2008.08.12 820
29 Defining classes and inheritance (클래스 정의와 상속) 황제낙엽 2011.03.24 392
28 JavaScript Closures for Dummies 황제낙엽 2009.04.08 227
27 [펌] 아사페릴의 사생활 - Code Conventions for the JavaScript Programming Language 황제낙엽 2009.04.02 194
26 자바스크립트의 데이터 타입과 변수 황제낙엽 2008.08.06 179
25 [펌] 아사페릴의 사생활 - Javascript의 constructor 와 prototype 황제낙엽 2009.04.02 156
24 재사용 가능한 일회용 객체 황제낙엽 2008.08.08 133
23 inherits() 를 이용한 상속 황제낙엽 2012.07.18 129
22 [펌] 아사페릴의 사생활 - 싱글톤 패턴을 지향한 Javascript Module Pattern 황제낙엽 2009.04.02 90
» 상속과 Super 로의 접근 황제낙엽 2012.09.18 64
20 체인 생성자(생성자 체인), 프로토타입 체인 그리고 생성자 재지정 황제낙엽 2009.08.12 55
19 [key:value] 형태로 object를 저장할 수 있는 Static영역의 해쉬맵 클래스 (Map) 황제낙엽 2008.11.04 46
18 [펌] 아사페릴의 사생활 - prototype과 __proto__ 와 constructor 황제낙엽 2009.04.02 41
17 생성자 체인과 상속 황제낙엽 2008.08.08 24
16 [펌] 아사페릴의 사생활 - __proto__ 와 construct 와 prototype 황제낙엽 2009.04.02 23
15 [펌]자바스크립트에서 객체 생성 방법 황제낙엽 2008.08.07 21
14 Object 와 Prototype 황제낙엽 2008.08.08 21
13 [펌] TAEYO.NET - JavaScript OOP 코어객체와 prototype를 사용한 객체확장 황제낙엽 2009.04.02 21
12 [펌]객체지향 자바스크립트 file 황제낙엽 2008.08.06 20
11 [펌]Function과 객체, this 키워드의 관계 황제낙엽 2008.08.07 19