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