์ƒˆ์†Œ์‹

Languages/Java Script

[Js] ์ƒ์† | javascript์ƒ์† | prototype

  • -
๋ฐ˜์‘ํ˜•

ํ•ด๋‹น ํฌ์ŠคํŒ…์€ "์ƒํ™œ์ฝ”๋”ฉ"์˜ ์ด๊ณ ์ž‰ ๋‹˜์˜ ๊ฐ•์˜๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ์˜ฌ๋ฆฐ ๊ฒŒ์‹œ๊ธ€ ์ž…๋‹ˆ๋‹ค.

์ƒ์—…์ ์ธ ์šฉ๋„๊ฐ€ ์•„๋‹Œ, ๊ฐœ์ธ ๊ณต๋ถ€ ์ •๋ฆฌ ๋ชฉ์ ์œผ๋กœ ์˜ฌ๋ฆฌ๋Š” ๊ธ€์ž„์„ ๋ฏธ๋ฆฌ ์•Œ๋ฆฝ๋‹ˆ๋‹ค.

 

 

 

 


 

์ƒ์†(inheritance)์ด๋ž€?

๊ฐ์ฒด๋Š” ์—ฐ๊ด€๋œ ๋กœ์ง๋“ค๋กœ ์ด๋ฃจ์–ด์ง„ ์ž‘์€ ํ”„๋กœ๊ทธ๋žจ์ด๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ๋‹ค.

์ƒ์†์€ ๊ฐ์ฒด์˜ ๋กœ์ง์„ ๊ทธ๋Œ€๋กœ ๋ฌผ๋ ค ๋ฐ›๋Š” ๋˜ ๋‹ค๋ฅธ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋Šฅ์„ ์˜๋ฏธํ•œ๋‹ค. ๋‹จ์ˆœํžˆ ๋ฌผ๋ ค๋ฐ›๋Š” ๊ฒƒ์ด๋ผ๋ฉด ์˜๋ฏธ๊ฐ€ ์—†์„ ๊ฒƒ์ด๋‹ค. ๊ธฐ์กด์˜ ๋กœ์ง์„ ์ˆ˜์ •ํ•˜๊ณ  ๋ณ€๊ฒฝํ•ด์„œ ํŒŒ์ƒ๋œ ์ƒˆ๋กœ์šด ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ค€๋‹ค.

 

์ฆ‰, ์‰ฝ๊ฒŒ ์„ค๋ช…ํ•˜์ž๋ฉด ๊ฐ์ฒด๋ž€ ๋ณ€์ˆ˜์™€ ๋ฉ”์†Œ๋“œ๋ฅผ ๋ฌถ์–ด๋†“์€ ๋ฌถ์Œ ์ด๋ผ๊ณ  ๋ณด๊ณ  ์ด๋Ÿฌํ•œ ๊ฐ์ฒด์˜ ์†์„ฑ์„ ๊ทธ๋Œ€๋กœ ๋ฌผ๋ ค๋ฐ›์€ ์ž๋…€๊ฐ์ฒด (๋ถ€๋ชจ๊ฐ์ฒด์˜ ๋ณ€์ˆ˜์™€ ๋ฉ”์†Œ๋“œ ๋ฅผ ๋ฌผ๋ ค๋ฐ›์Œ) ๋ฅผ ๋งŒ๋“ค๊ณ , ์ˆ˜์ •ํ•˜๊ณ  ๋ณ€๊ฒฝ๊ฐ€๋Šฅํ•œ ๊ฒƒ์ด ๋ฐ”๋กœ ์ƒ์†์ธ ๊ฒƒ์ด๋‹ค. 

 

 

์•„๋ž˜ ์ฝ”๋“œ๋Š” ์ด์ „ ์‹œ๊ฐ„์— ์‚ดํŽด๋ณธ ์ฝ”๋“œ๋‹ค.

<script>
    function Person(name){
        this.name = name;
        this.introduce = function(){
            return 'My name is '+this.name; 
        }   
    }
    var p1 = new Person('haeun');
    document.write(p1.introduce()+"<br />");
</script>

์œ„ ์ฝ”๋“œ์˜ ๊ฒฐ๊ณผ๋Š” : My name is haeun์ด๋‹ค.

 

์œ„ ์ฝ”๋“œ๋ฅผ ์•„๋ž˜์™€ ๊ฐ™์ด ๋ฐ”๊พธ์–ด ๋ณด์ž!

 

๊ฒฐ๊ณผ๋Š” ๊ฐ™์ง€๋งŒ ์ด ์ฝ”๋“œ๋Š” ์ƒ์†์„ ์ค€๋น„ํ•˜๋Š” ๊ณผ์ •์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค.

<script>
    function Person(name){
        this.name = name;
    }
    Person.prototype.name=null;
    Person.prototype.introduce = function(){
        return 'My name is '+this.name; 
    }
    var p1 = new Person('haeun');
    document.write(p1.introduce()+"<br />");
</script>

์ƒ์„ฑ์ž ๋ฐ–์—์„œ prototype์ด๋ผ๋Š” ํŠน์ˆ˜ํ•œ ์•ฝ์†๋œ ์†์„ฑ์— name์„ ์ค€ ๊ฒƒ์ด๋‹ค.

 

 

์ƒ์†์„ ํ•˜๊ธฐ ์œ„ํ•œ ๊ธฐ๋ณธ์ ์ธ ์ค€๋น„๋ฅผ ๋งˆ์ณค์œผ๋‹ˆ, ์ด์ œ ์ƒ์†์„ ํ•ด๋ณด์ž!

์•„๋ž˜ ์ฝ”๋“œ๋Š” programmer๋ผ๋Š” ๊ฐ์ฒด๋ฅผ Person์— ์ƒ์†ํ•˜์—ฌ, introduce๋ผ๋Š” ์—†๋Š” ์†์„ฑ์„ ์‚ฌ์šฉํ•˜๋Š” ์˜ˆ์ œ์ด๋‹ค.

<script>
      function Person(name){
          this.name = name;
      }
      Person.prototype.name=null;
      Person.prototype.introduce = function(){
          return 'My name is '+this.name; 
      }

      function Programmer(name){
          this.name = name;
      }
      Programmer.prototype = new Person();

      var p1 = new Programmer('haeun');
      document.write(p1.introduce()+"<br />");
</script>

 

Programmer์ด๋ผ๋Š” ์ƒ์„ฑ์ž๋ฅผ ๋งŒ๋“ค์—ˆ๋‹ค.

 

๊ทธ๋ฆฌ๊ณ  ์ด ์ƒ์„ฑ์ž์˜ prototype๊ณผ Person์˜ ๊ฐ์ฒด๋ฅผ ์—ฐ๊ฒฐํ–ˆ๋”๋‹ˆ Programmer ๊ฐ์ฒด๋„ ๋ฉ”์†Œ๋“œ introduce๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ๋‹ค. Programmer๊ฐ€ Person์˜ ๊ธฐ๋Šฅ์„ ์ƒ์†ํ•˜๊ณ  ์žˆ๋Š” ๊ฒƒ์ด๋‹ค.

 

๋‹จ์ˆœํžˆ ๋˜‘๊ฐ™์€ ๊ธฐ๋Šฅ์„ ๊ฐ–๊ฒŒ ๋˜๋Š” ๊ฒƒ์ด๋ผ๋ฉด ์ƒ์†์˜ ์˜์˜๋Š” ์‚ฌ๋ผ์งˆ ๊ฒƒ์ด๋‹ค. ๋ถ€๋ชจ์˜ ๊ธฐ๋Šฅ์„ ๊ณ„์Šน ๋ฐœ์ „ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด ์ƒ์†์˜ ๊ฐ€์น˜๋‹ค.

 

 

์œ„์˜ ์ฝ”๋“œ๊ฐ€ ์ž˜ ์ดํ•ด๊ฐ€ ๋˜์ง€ ์•Š๋Š”๋‹ค๋ฉด ์•„๋ž˜ ์˜์ƒ์„ ์ฐธ๊ณ ํ•ด๋ณด์ž.

www.youtube.com/watch?v=mM-4t2J0lYM&feature=emb_logo

์‰ฝ๊ฒŒ ๋งํ•˜์ž๋ฉด ์ƒ์†ํ•  (๊ธฐ๋Šฅ์„ ๊ฐ€์ ธ์˜ค๊ณ  ์‹ถ์€) ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑ์ž๋กœ ๋งŒ๋“  ๊ฒƒ์„ 

์ƒˆ๋กœ ๋งŒ๋“ค ๊ฐ์ฒด์— prototype๊ฐ’์œผ๋กœ ์ฃผ๋ฉด ๋˜๋Š” ๊ฒƒ์ด๋‹ค.

 

์ด์ „ ์ฝ”๋“œ๋ฅผ ์กฐ๊ธˆ ๋ฐ”๊พธ์–ด, ์ƒ์† ๋ฟ ์•„๋‹ˆ๋ผ ๊ทธ ๊ธฐ๋Šฅ์„ ์ˆ˜์ • ํ˜น์€ ๋ณ€๊ฒฝํ•˜๋Š” ๊ณผ์ •์„ ์‚ดํŽด๋ณด๋„๋ก ํ•˜์ž.

<!DOCTYPE html>
<html>
<head>
	<title>์ƒ์„ฑ์ž</title>
</head>
<body>
<script type="text/javascript">
    function Person(name){//Person์€ ๋ถ€๋ชจ ๊ฐ์ฒด๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ์Œ
        this.name = name;
    }
    Person.prototype.name=null;
    Person.prototype.introduce = function(){
        return 'My name is '+this.name; 
    }

    function Programmer(name){ //Programmer์ด๋ผ๋Š” ๊ฐ์ฒด ์ƒ์„ฑ
        this.name = name;
    }
    Programmer.prototype = new Person(); // ์ƒ์†๋  ์ƒ์„ฑ์ž๋ฅผ ๋‹ด๊ณ  ์žˆ์Œ 
    Programmer.prototype.coding = function(){
        return "hello world";
    }

    var p1 = new Programmer('haeun');
    document.write(p1.introduce()+"<br />");
    document.write(p1.coding()+"<br />");



    function Designer(name){
        this.name = name;
    }
    Designer.prototype = new Person(); 
    Designer.prototype.design = function(){
        return "beautiful~!!";
    }
    
    var p2 = new Designer('admin');
    document.write(p2.introduce()+"<br />");
    document.write(p2.design()+"<br />");

</script>
</body>
</html>

 

๊ฒฐ๊ณผ๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

์ด๋ ‡๊ฒŒ ๋ถ€๋ชจ์—๊ฒŒ ์ƒ์†๋ฐ›์€ ๊ธฐ๋Šฅ์„ ๋ณ€๊ฒฝํ•˜๊ฑฐ๋‚˜ ์ถ”๊ฐ€ํ•  ์ˆ˜๋„ ์žˆ๋Š” ๊ฒƒ์ด๋‹ค.

 

์ƒ์†์—์„œ ๊ฐ€์žฅ ํ•ต์‹ฌ์ ์ธ ์—ญํ• ์„ ๋‹ด๋‹นํ•œ ๊ฒƒ์€ prototype์ธ๋ฐ, ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋Š” 'prototype based language' ๋ผ๊ณ ๋„ ํ•œ๋‹ค.

๊ทธ๋งŒํผ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ(๊ฐ์ฒด,ํ•จ์ˆ˜ ๋“ฑ)์—์„œ ๋งค์šฐ ์ค‘์š”ํ•œ ํƒ€์ž…์ด๋‹ค.

๋ฐ˜์‘ํ˜•
Contents

ํฌ์ŠคํŒ… ์ฃผ์†Œ๋ฅผ ๋ณต์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค

์ด ๊ธ€์ด ๋„์›€์ด ๋˜์—ˆ๋‹ค๋ฉด ๊ณต๊ฐ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.