자바스크립트2013. 2. 8. 10:16

자바스크립트 퀴즈

http://blog.javarouka.me/



Posted by 선한열심
자바스크립트2013. 2. 7. 17:54

출처 - http://blog.naver.com/oaie/140172333031


이거 이해 하는데 하루종일 걸리네

 

우선 동물 클래스를 만들고 원숭이,개,고양이를 만들어 보자.

 

function Animal(name) {

    this.name = name;

 

    this.getName = function () {

        alert(this.name);

    }

}

 

var monkey = new Animal("Monkey");

var dog = new Animal("dog");

var cat = new Animal("cat");

 

monkey.getName();

dog.getName();

cat.getName();

 

 

잘만들어 지고 alert로 Monkey,dog,cat 세번 띄우겠지..

 

이렇게 하면 객체마다 this.name과 this.getName() 을 갖게 된다.

 

 

 

 

 

 

 

 Object.prototype.getName = function () {

    alert(this.name);

}

 

function Animal(name) {

    this.name = name;

}

 

var monkey = new Animal("Monkey");

var dog = new Animal("dog");

var cat = new Animal("cat");

 

monkey.getName();

dog.getName();

cat.getName();

 

 

 이렇게 하면 각 객체들은 getName 메소드는 가지고 있지 않고.

객체가 getName메소드를 호출할때 Object.prototype.getName을 참조하여 메소드를 호출하게 된다.

Object.prototype 메소드를 한번만 만들기 때문에 메모리 효율이 더 좋아진다.

 

 

 

monkey.getName했을 때 우선 monkey.prototype.getName 이 있는지 없는지 확인하고 없으면 Object.prototype.getName 을 참조한다.

있으면 리턴해서 getName()를 호출한다.

 

Object.prototype.getName하나만 만들었기 때문에 각 객체에 getName메소드가 있는 것 보다 메모리 효율이 좋을 것이다.....

 

라고 나는 이렇게 이해한다.

 

 

Posted by 선한열심
자바스크립트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 선한열심