자바스크립트2013. 2. 6. 15:17

출처 : http://kuimoani.egloos.com/1272779



<html>

<script type="text/javascript"> 

    

    

    

function BaseClass () {    

    this.name = "base name";    

    this.type = "base type";    

    this.Run = function () {        

        alert("Run Base = " + this.name);    

    }    

    this.Exec = function () {        

        alert("Exec Base = " + this.name);    

    }

}

            

function SubClass () {    

    this.base = new BaseClass();    

    this.name = "sub name";    

    this.Run = function () {        

        alert("Run Sub = " + this.name);        

        this.base.Run();    

    }

}


SubClass.prototype = new BaseClass();

var a = new SubClass();

var msg = "";


msg += a.name;               // print "sub name"

msg += a.type;               // print "base type"

alert(msg);   

msg += a.Run();              // print "Run Sub = sub name" , "Run Base = base name"

msg += a.Exec();             // print "Exec Base = sub name"

// 

    

    

    

    

function Base(){    

    this.name = "base";    

    this.type = "human";    

    this.getName = function () {        

        return this.name + " is base";    

    }

}


function Child(){    

    this.name = "child";    

    this.getName = function () {        

        this.name = "child2";        

        return this.name + " " + this.$getName() + " " + this.type;    

    }

}


function Extend(child, base) {    

    child.prototype = new base();       //상속    

    var objBase = new base();    

    var objChild = new child();    //base 객체 생성    

    for(var item in objBase) {        

        if(typeof(objBase[item]) == "function")            

        objChild["$" + item] = objBase[item];    

    }    

    return objChild;

}


var obj = Extend(Child, Base);

alert(obj.getName());


//alert : child2 child2 is base human....



</script>


Posted by 선한열심