ARCHIVE/JavaScript

[JS] ๊ฐ์ฒด์ง€ํ–ฅ 2 ( Create, Class )

man_on 2021. 12. 28. 22:32
๋ฐ˜์‘ํ˜•

 

 

 

     


    ๐Ÿ”—  Prototype ์ •๋ฆฌ

    https://manon-kim.tistory.com/129

     

    [JS] ๊ฐ์ฒด์ง€ํ–ฅ (Constructor, Prototype)

     Prototype ?  : Javascript๋Š” ํ”ํžˆ ํ”„๋กœํ† ํƒ€์ž… ๊ธฐ๋ฐ˜์–ธ์–ด(prototype-based language)๋ผ ๋ถˆ๋ฆฐ๋‹ค.  ๋ชจ๋“  ๊ฐ์ฒด๋“ค์ด ๋ฉ”์†Œ๋“œ์™€ ์†์„ฑ๋“ค์„ ์ƒ์†๋ฐ›๊ธฐ ์œ„ํ•œ ํ…œํ”Œ๋ฆฟ์œผ๋กœ ํ”„๋กœํ† ํƒ€์ž… ๊ฐ์ฒด(prototype object)๋ฅผ ๊ฐ€์ง„๋‹ค...

    manon-kim.tistory.com

     

     

     


     

    ES5  -  Object.create( )

     

     

    Object.create()

    : ES5 ๋•Œ ๋‚˜์˜จ ์ƒ์†์ด์šฉํ•˜์—ฌ ์‰ฝ๊ฒŒ object ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•

     

    Object.create(๋ถ€๋ชจobject) : ์†Œ๊ด„ํ˜ธ์•ˆ์— ์ ์€ ๋ถ€๋ชจ object๊ฐ€ prototype์ด ๋œ๋‹ค.

    let ๋ถ€๋ชจ = { name : 'Kim', age : 50 };
    let ์ž์‹ = Object.create(๋ถ€๋ชจ);
    
    console.log(์ž์‹.age);
    //์ถœ๋ ฅ : 50

     

     

    ์ž์‹์ด ์†์„ฑ์„ ๋ฐ”๊พธ๊ณ ์‹ถ์œผ๋ฉด ๊ฐ’์„ ์ƒˆ๋กœ ๋ถ€์—ฌํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

    let ๋ถ€๋ชจ = { name : 'Kim', age : 50 };
    let ์ž์‹ = Object.create(๋ถ€๋ชจ);
    ์ž์‹.age  = 20;
    
    console.log(์ž์‹.age); 
    //์ถœ๋ ฅ : 20

     

     ? ์™œ ๋ถ€๋ชจ์˜ ์ƒ์†๊ฐ’์ด ์ถœ๋ ฅ๋˜์ง€์•Š์Œ ?

     : javascript object ์ž๋ฃŒํ˜•์—์„œ ํŠน์ • ์ž๋ฃŒ๋ฅผ ๊บผ๋‚ผ ๋•Œ ๋ฌป๋Š” ์ˆœ์„œ๊ฐ€

     1. ํ•ด๋‹น object๊ฐ€ ์ง์ ‘ ๊ทธ ๊ฐ’์„ ๊ฐ€์ง€๊ณ ์žˆ์œผ๋ฉด ๊ทธ๊ฑฐ ์ถœ๋ ฅ

     2. ์—†์œผ๋ฉด ๋ถ€๋ชจ์˜ prototype ์—์„œ ์ฐพ์•„์„œ ์ถœ๋ ฅ

     3. ๋˜ ์—†์œผ๋ฉด ๋ถ€๋ชจ์˜ ๋ถ€๋ชจ (...๋ฐ˜๋ณต...) ํ•ด์„œ ์ถœ๋ ฅํ•œ๋‹ค.

     

     

     

    ์ด๋Ÿฐ์‹์œผ๋กœ ์ž์‹์˜ ์†์ฃผ๊นŒ์ง€ ๊ฐ€๋Šฅ~

    let ๋ถ€๋ชจ = { name : 'Kim', age : 50 };
    let ์ž์‹ = Object.create(๋ถ€๋ชจ);
    ์ž์‹.age  = 20;
    
    let ์†์ž = Object.create(์ž์‹);
    
    console.log(์†์ž.age);
    // ์ถœ๋ ฅ : 20

     

     

     

     

     

     

     

     

     

     

     

    ES6  -  Class / construtor

     

     

    class / construtor

    : ES6 class ์‹ ๋ฌธ๋ฒ•์œผ๋กœ constructor ์ƒ์„ฑ

      ์ด๋ ‡๊ฒŒ class๋กœ๋ถ€ํ„ฐ ์ƒˆ๋กœ ์ƒ์„ฑ๋œ object๋ฅผ instance๋ผ๊ณ  ํ•œ๋‹ค.

    class ๋ถ€๋ชจ {
      constructor(){
        this.name = 'Kim'
      }
    }
    
    let ์ž์‹ = new ๋ถ€๋ชจ();

     

     

     

    ์ƒ์†๊ฐ€๋Šฅํ•œ ํ•จ์ˆ˜ ์ถ”๊ฐ€ ๋ฐฉ๋ฒ• 2๊ฐ€์ง€

     

    1. constructor ์•ˆ์— ์ถ”๊ฐ€

    class ๋ถ€๋ชจ {
      constructor(){
        this.name = 'Kim';
        this.sayHi = function(){ console.log('hello') }
      }
    }
    
    let ์ž์‹ = new ๋ถ€๋ชจ();

     

     

    2. prototype์— ์ถ”๊ฐ€

       : object์— ํ•จ์ˆ˜ ์ถ”๊ฐ€ ํ•˜๋“ฏ์ด ํ•˜๊ฑฐ๋‚˜

    class ๋ถ€๋ชจ {
      constructor(์ด๋ฆ„, ๋‚˜์ด){
        this.name = ์ด๋ฆ„;
        this.age = ๋‚˜์ด;
      }
      sayHi(){
        console.log('Hi');
      }
      sayHello(){
        console.log('Hello');
      }
    }
    
    let ์ž์‹ = new ๋ถ€๋ชจ('Park', 28);

     

      ๋ถ€๋ชจ.prototype.sayHi = function ( ) { } ๋กœ ํ•ด๋„๋œ๋‹ค.

     

     

     

     

     

    Class - extends / super

     

     

     

    ๐Ÿ”ป ๋‹ค๋ฅธ class๋ฅผ ์ƒ์†ํ•ด์„œ ์ƒˆ๋กœ์šด class๋ฅผ ๋งŒ๋“ค ๋•Œ extends๋ฅผ ์“ด๋‹ค.

    extendsํ•œ class์—์„œ๋Š” super( )๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค.

    super๋Š” 'extends๋กœ ์ƒ์†์ค‘์ธ ๋ถ€๋ชจ class์˜ constructor()'์„ ์˜๋ฏธํ•œ๋‹ค.

     

     

    super์—๋Š” ์ƒ์†๋ฐ›์€ class์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๊ทธ๋Œ€๋กœ ์ถ”๊ฐ€ํ•ด์ค˜์•ผํ•œ๋‹ค. (ํŒŒ๋ผ๋ฏธํ„ฐ ์ž‘๋ช…์€ ์ž์œ ๋กญ๊ฒŒ ๊ฐ€๋Šฅ)

    // ๊ธฐ์กด class
    class dog {
      constructor(a, b) {
        this.type = a;
        this.color = b;
      }
    }
    
    // ๊ธฐ์กด dog class๋ฅผ ๊ฐ€์ ธ์™€์„œ ์ƒˆ๋กœ์šด class ์ƒ์„ฑ
    class cat extends dog {
      constructor(c) {
        super();
        this.age = c;
      }
    }

     

     

    class๊ฐ„์— ํ•จ์ˆ˜ ์ƒ์†๋ฐ›๊ธฐ

    : ๋˜‘๊ฐ™์ด super๋กœ ๊ฐ€์ ธ์˜จ๋‹ค.

    class dog {
      constructor(a, b) {
        this.type = a;
        this.color = b;
      }
      sayHello() {
        console.log("์•ˆ๋…• ๋‚˜๋Š” ๊ฐœ");
      }
    }
    
    class cat extends dog {
      constructor(a, b, c) {
        super(a, b);
        this.age = c;
      }
      sayHello2() {
        console.log("์•ˆ๋…• ๋‚˜๋Š” ๊ณ ์–‘์ด");
        super.sayHello();
      }
    }
    
    let cat1 = new cat("์ฝ”์ˆ", "whtie", 5);
    
    console.log(cat1.sayHello2());
    //์ถœ๋ ฅ : ์•ˆ๋…• ๋‚˜๋Š” ๊ณ ์–‘์ด, ์•ˆ๋…• ๋‚˜๋Š” ๊ฐœ

     

     

     

    ๋ฐ˜์‘ํ˜•

    'ARCHIVE > JavaScript' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

    [JS] ๋น„๋™๊ธฐ / Promise / async.await  (0) 2022.01.06
    [JS] getter,setter / import,export  (0) 2022.01.05
    [JS] ๊ฐ์ฒด์ง€ํ–ฅ (Constructor, Prototype)  (0) 2021.12.28
    [JS] this  (0) 2021.11.13
    [JS] Error (Try catch & throw)  (0) 2021.09.22