Prototype 상속과 Super 로의 접근

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

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 영역에 정의해야만 한다

번호 제목 글쓴이 날짜 조회 수
» 상속과 Super 로의 접근 황제낙엽 2012.09.18 389
29 inherits() 를 이용한 상속 황제낙엽 2012.07.18 423
28 Defining classes and inheritance (클래스 정의와 상속) 황제낙엽 2011.03.24 655
27 JavaScript Closures for Dummies 황제낙엽 2009.04.08 651
26 체인 생성자(생성자 체인), 프로토타입 체인 그리고 생성자 재지정 황제낙엽 2009.08.12 365
25 [펌] TAEYO.NET - Js OOP - 나만의 프레임워크 만들기 황제낙엽 2009.04.02 310
24 [펌] TAEYO.NET - Js OOP - 사용자 정의 객체. 그리고 상속과 재사용 황제낙엽 2009.04.02 322
23 [펌] TAEYO.NET - JavaScript OOP 코어객체와 prototype를 사용한 객체확장 황제낙엽 2009.04.02 329
22 [펌] 아사페릴의 사생활 - 싱글톤 패턴을 지향한 Javascript Module Pattern 황제낙엽 2009.04.02 407
21 [펌] 아사페릴의 사생활 - Code Conventions for the JavaScript Programming Language 황제낙엽 2009.04.02 469
20 [펌] 아사페릴의 사생활 - __proto__ 와 construct 와 prototype 황제낙엽 2009.04.02 316
19 [펌] 아사페릴의 사생활 - prototype과 __proto__ 와 constructor 황제낙엽 2009.04.02 364
18 [펌] 아사페릴의 사생활 - __proto__ 와 prototype에 대해.. 황제낙엽 2009.04.02 306
17 [펌] 아사페릴의 사생활 - Javascript의 클래스에 관한 이야기 황제낙엽 2009.04.02 345
16 [펌] 아사페릴의 사생활 - Javascript의 constructor 와 prototype 황제낙엽 2009.04.02 453
15 [펌] prototype (2) 황제낙엽 2009.04.02 302
14 [펌] prototype (1) 황제낙엽 2009.04.02 314
13 [key:value] 형태로 object를 저장할 수 있는 Static영역의 해쉬맵 클래스 (Map) 황제낙엽 2008.11.04 369
12 함수와 인자값 (arguments) 황제낙엽 2008.08.12 320
11 중첩 함수, 함수 클로저 황제낙엽 2008.08.12 1220