Prototype 상속과 Super 로의 접근

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

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 1278
29 Defining classes and inheritance (클래스 정의와 상속) 황제낙엽 2011.03.24 734
28 JavaScript Closures for Dummies 황제낙엽 2009.04.08 708
27 [펌] 아사페릴의 사생활 - Code Conventions for the JavaScript Programming Language 황제낙엽 2009.04.02 512
26 [펌] 아사페릴의 사생활 - Javascript의 constructor 와 prototype 황제낙엽 2009.04.02 511
25 자바스크립트의 데이터 타입과 변수 황제낙엽 2008.08.06 505
24 재사용 가능한 일회용 객체 황제낙엽 2008.08.08 484
23 inherits() 를 이용한 상속 황제낙엽 2012.07.18 476
22 [펌] 아사페릴의 사생활 - 싱글톤 패턴을 지향한 Javascript Module Pattern 황제낙엽 2009.04.02 455
» 상속과 Super 로의 접근 황제낙엽 2012.09.18 441
20 [key:value] 형태로 object를 저장할 수 있는 Static영역의 해쉬맵 클래스 (Map) 황제낙엽 2008.11.04 414
19 [펌] 아사페릴의 사생활 - prototype과 __proto__ 와 constructor 황제낙엽 2009.04.02 414
18 체인 생성자(생성자 체인), 프로토타입 체인 그리고 생성자 재지정 황제낙엽 2009.08.12 411
17 [펌] 아사페릴의 사생활 - Javascript의 클래스에 관한 이야기 황제낙엽 2009.04.02 398
16 [펌] TAEYO.NET - JavaScript OOP 코어객체와 prototype를 사용한 객체확장 황제낙엽 2009.04.02 376
15 [펌] TAEYO.NET - Js OOP - 나만의 프레임워크 만들기 황제낙엽 2009.04.02 375
14 [펌] prototype (1) 황제낙엽 2009.04.02 374
13 함수와 인자값 (arguments) 황제낙엽 2008.08.12 371
12 [펌]JavaScript Class 만들기 황제낙엽 2008.08.07 369
11 [펌] 아사페릴의 사생활 - __proto__ 와 construct 와 prototype 황제낙엽 2009.04.02 365