๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐ŸŒŒ | WEB DEV/Vanilla JS

JS_๊ธฐ๋ณธ ๋ฌธ๋ฒ• (26)_ ์ ‘๊ทผ์ž ํ”„๋กœํผํ‹ฐ์™€ privateํ•„๋“œ

by KASSID 2023. 4. 10.

๋ชฉ์ฐจ

    728x90

    JS์˜ ๊ฐ์ฒด ํ”„๋กœํผํ‹ฐ ์ค‘ ์ ‘๊ทผ์ž ํ”„๋กœํผํ‹ฐ์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด์ž!

     

    1. ์ ‘๊ทผ์ž ํ”„๋กœํผํ‹ฐ

    ์ ‘๊ทผ์ž ํ”„๋กœํผํ‹ฐ๋Š” ์Šค์Šค๋กœ ๊ฐ’์„ ๊ฐ–์ง€ ์•Š๊ณ 

    ๋‹ค๋ฅธ ํ”„๋กœํผํ‹ฐ์˜ ๊ฐ’์„ ์ฝ๊ฑฐ๋‚˜(get) ์ €์žฅํ•  ๋•Œ(set) ์‚ฌ์šฉํ•œ๋‹ค.

     

    ๋‹ค๋ฅธ ์–ธ์–ด์—์„œ๋„ ์ฐพ์•„๋ณผ ์ˆ˜ ์žˆ๊ณ  getterํ•จ์ˆ˜, setterํ•จ์ˆ˜๋ผ๊ณ  ๋ถ€๋ฅด๊ธฐ๋„ ํ•œ๋‹ค!


    getter

    - ๋ฐ˜๋“œ์‹œ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•ด์•ผ ํ•œ๋‹ค.

    - ํŠน์ • ํ”„๋กœํผํ‹ฐ๋ฅผ ์›ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ๊ฐ€๊ณตํ•˜์—ฌ ๋‚ด๋ณด๋‚ผ ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.

    setter

    - ํŠน์ • ํ”„๋กœํผํ‹ฐ์— ๊ฐ’์ด ์ €์žฅ๋˜๋Š” ๋ฐฉ์‹์„ ์กฐ์ž‘ํ•˜๊ฑฐ๋‚˜ ์ œ์•ฝ์‚ฌํ•ญ์„ ์ ์šฉํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.

    - ํ•˜๋‚˜์˜ ์ธ์ž๋ฅผ ๋ฐ›๋Š”๋‹ค.

     

    ์‚ฌ์šฉ๋ฐฉ๋ฒ•

    ๋ฐฉ๋ฒ• 1) ๊ฐ์ฒด ๋ฆฌํ„ฐ๋Ÿด ๋ฐฉ์‹

    ์˜ˆ์‹œ 1) ๋ฌผ์ฒด์˜ ๊ธธ์ด ๋‹จ์œ„ (์ธ์น˜์™€ ์„ผํ‹ฐ๋ฏธํ„ฐ)

    const obj1 = {
      inch : 1,
      
      get cm() {
        return this.inch * 2.54;
      },
      
      set cm(temp) {
        this.inch = Number((temp * 0.393701).toFixed(2));
      }
    }

    ๊ฐ’์ด๋‚˜ ์ฐธ์กฐ๊ฐ’, ํ•จ์ˆ˜ ๋“ฑ์„ ๋‹ด๋Š” ๋ฐ์ดํ„ฐ ํ”„๋กœํผํ‹ฐ ์ฆ‰, ์ผ๋ฐ˜์ ์ธ ํ”„๋กœํผํ‹ฐ์™€ ๋‹ค๋ฅด๊ฒŒ

    get, set์ด ์•ž์— ๋ถ™์–ด์žˆ๋Š” ๊ฒƒ์ด ๋ฐ”๋กœ ์ ‘๊ทผ์ž ํ”„๋กœํผํ‹ฐ์ด๋‹ค.

     

    ์ฃผ์˜ํ•ด์•ผํ•  ์ ์€ ํ•จ์ˆ˜์˜ ์ƒ๊น€์ƒˆ๋ฅผ ๋„๊ณ  ์žˆ์ง€๋งŒ

    ์ผ๋ฐ˜ ํ”„๋กœํผํ‹ฐ์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•ด์•ผํ•œ๋‹ค๋Š” ์ ์ด๋‹ค!

     

    ํ•œ ๋ฒˆ ํ˜•ํƒœ๋ฅผ ์‚ดํŽด๋ณด๋ฉด

    console.log(obj1)

    ์ด์ฒ˜๋Ÿผ ๊ฐ์ฒด ์•ˆ์— get, set์ด๋ผ๋Š” ํ‚ค์›Œ๋“œ์™€ ํ•จ๊ป˜ ์กด์žฌํ•˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

     

     

    ์˜ˆ์‹œ 1-1) getterํ•จ์ˆ˜

    console.log(obj1.inch)
    console.log(obj1.cm)
    
    
    >>> 1
    >>> 2.54

     

    ์˜ˆ์‹œ 1-2) setter ํ•จ์ˆ˜

    // setter ํ•จ์ˆ˜ ์—ญ์‹œ ํ”„๋กœํผํ‹ฐ์ฒ˜๋Ÿผ ์‚ฌ์šฉ
    obj.cm = 20
    
    console.log(obj1.inch)
    
    >>> 7.87

    getter์™€ setterํ•จ์ˆ˜๋Š” ์ ‘๊ทผ์ž "ํ”„๋กœํผํ‹ฐ"์ด๋‹ค.

    ๋”ฐ๋ผ์„œ ์œ„์˜ ์˜ˆ์‹œ์ฒ˜๋Ÿผ ์ผ๋ฐ˜์ ์ธ ํ”„๋กœํผํ‹ฐ์™€ ๋™์ผํ•œ ํ˜•ํƒœ๋กœ ์‚ฌ์šฉํ•˜๋ฉด๋œ๋‹ค.

     

     

    ๋ฐฉ๋ฒ• 2) ํด๋ž˜์Šค์—์„œ ์‚ฌ์šฉ

    ํด๋ž˜์Šค์—์„œ๋„ ์ ‘๊ทผ์ž ํ”„๋กœํผํ‹ฐ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

    ์—ญ์‹œ ๋™์ผํ•˜๊ฒŒ get, set์„ ์•ž์— ๋ถ™์—ฌ์„œ ์ •์˜ํ•œ๋‹ค.

    class Player {
      constructor (name, job, power = 1){
        this.name = name;
        this.job = job;
        this.power = power;
      }
      get info() {
        return `์ด๋ฆ„ : ${this.name} | ์ง์—…๊ตฐ : ${this.job}`;
      }
      set powerStat(powerStat) {
        if (typeof powerStat !== 'number') return;
        if (powerStat < 0) return;
        this.power = powerStat;
      }
    }
    
    
    const p1 = new Player('Kassid', '์ „์‚ฌ');

     

    get ํ•จ์ˆ˜๋Š” ๊ฐ์ฒด๋ฆฌํ„ฐ๋Ÿด ๋ฐฉ์‹๊ณผ ๋™์ผํ•˜๊ฒŒ ์‚ฌ์šฉํ•œ๋‹ค!

    console.log(p1.info);
    
    >>> ์ด๋ฆ„ : Kassid | ์ง์—…๊ตฐ : ์ „์‚ฌ

     

    set ํ•จ์ˆ˜๋Š” ํ• ๋‹น๋ฐ›์€ ๊ฐ’์˜ ์ฒ˜๋ฆฌ ์™ธ์—๋„ ์ œ์•ฝ์‚ฌํ•ญ์„ ์ค„ ๋•Œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

     

    ์˜ˆ์‹œ 1) ์ •์ƒ์ ์œผ๋กœ ํ• ๋‹นํ•œ ๊ฒฝ์šฐ

    p1.powerStat = 10
    console.log(p1);
    
    >>> Player {name: 'Kassid', job: '์ „์‚ฌ', power: 10}

     

    ์˜ˆ์‹œ 2) ๋น„์ •์ƒ์ ์œผ๋กœ ํ• ๋‹นํ•œ ๊ฒฝ์šฐ (์ œ์•ฝ์‚ฌํ•ญ ์œ„๋ฐ˜)

    p1.powerStat = -1
    console.log(p1);
    
    >>> Player {name: 'Kassid', job: '์ „์‚ฌ', power: 10}

     

    ํด๋ž˜์Šค ๋‚ด์˜ setterํ•จ์ˆ˜์—์„œ ์ œ์•ฝ์‚ฌํ•ญ์„ ๋„ฃ๊ณ 

    ๊ทธ๊ฒƒ์„ ์œ„๋ฐ˜ํ•  ๊ฒฝ์šฐ return ํ•˜๋„๋กํ•˜์˜€๊ธฐ ๋•Œ๋ฌธ์—

    ์œ„์˜ ์˜ˆ์‹œ์—์„œ ์ž…๋ ฅํ•œ ๊ฐ’์ด ํ• ๋‹น๋˜์ง€ ์•Š์Œ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

     

    ๋˜ํ•œ getter ์™€ setter๋Š” ํด๋ž˜์Šค์— ์กด์žฌํ•  ๊ฒฝ์šฐ

    [[Prototype]]์ด ๋œ๋‹ค.


    ์ž ์—ฌ๊ธฐ์„œ ์ž ๊น!
    setterํ•จ์ˆ˜๋Š” ๊ฐ’์„ ํ• ๋‹นํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š”๋ฐ  ์‹ค์ œ ํ•„๋“œ๋ช…๊ณผ ๋™์ผํ•˜๊ฒŒ ์‚ฌ์šฉํ•˜๋ฉด ์•ˆ๋ ๊นŒ?

    ๋ผ๋Š” ์ƒ๊ฐ์„ ํ•˜๊ฒŒ ๋œ๋‹ค.

     

    ๊ณผ์—ฐ ๊ฐ€๋Šฅํ• ๊นŒ?

     

    ํ•„๋“œ๋ช…๊ณผ setter์˜ ์ด๋ฆ„์ด ๊ฐ™์€ ๊ฒฝ์šฐ

     

    ์˜ˆ์‹œ) ํ•„๋“œ๋ช…๊ณผ setter๋ช…์ด ๊ฐ™์€ ํด๋ž˜์Šค

    class Player {
      constructor (name, job, power = 1){
        this.name = name;
        this.job = job;
        this.power = power;
      }
      get info() {
        return `์ด๋ฆ„ : ${this.name} | ์ง์—…๊ตฐ : ${this.job} | ํž˜ : ${this.power}`;
      }
      set power(power) {
        this.power = power;
      }
    }
    
    
    const p1 = new Player('Kassid', '์ „์‚ฌ');

    ์œ„์™€ ๊ฐ™์ด ๋‘ ์ด๋ฆ„์ด ๊ฐ™์€ ํด๋ž˜์Šค๋ฅผ ์ •์˜ํ•˜์˜€๋‹ค.

     

    ํ•˜์ง€๋งŒ ์ด๋Š” ์•ˆํƒ€๊น๊ฒŒ๋„ ๋‘ ์ด๋ฆ„์„ ์‹๋ณ„ํ•˜์ง€ ๋ชปํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

     

    ์˜ค๋ฅ˜ ๋ฐœ์ƒ ์›์ธ

    โ‘  constructor์˜ "this.power"๊ฐ€ setter ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.

    โ‘ก setterํ•จ์ˆ˜ ๋‚ด์˜ this.power๊ฐ€ setter ํ•จ์ˆ˜๋ฅผ ์žฌ๊ท€ํ˜ธ์ถœํ•œ๋‹ค.

    โ‘ข ๋ฌดํ•œ ๋ฐ˜๋ณต ์—๋Ÿฌ๋ฐœ์ƒ!

     

    ์ด๋Ÿฌํ•œ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์–ด๋–ป๊ฒŒ ํ•ด์•ผํ• ๊นŒ?

     

    ํ•ด๊ฒฐ์ฑ…

    ์ด๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ํ•„๋“œ๋ช…๊ณผ setter ํ•จ์ˆ˜๋ช…์„ ๋‹ค๋ฅด๊ฒŒ ํ•ด์•ผํ•œ๋‹ค.

    ํ•˜์ง€๋งŒ ์‚ฌ์šฉ์ž ์ž…์žฅ์—์„œ๋Š” ๋™์ผํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ ๋‹ค๋ฉด

    " _ํ•„๋“œ๋ช… " ๊ณผ ๊ฐ™์€ ํ˜•ํƒœ๋ฅผ ์ด์šฉํ•ด ๊ตฌํ˜„ํ•  ์ˆ˜์žˆ๋‹ค.

    class Player {
      constructor (name, job, power = 1){
        this.name = name;
        this.job = job;
        this.power = power; //setterํ•จ์ˆ˜์— argument ํ• ๋‹น
      }
      get info() {
        return `์ด๋ฆ„ : ${this.name} | ์ง์—…๊ตฐ : ${this.job} | ํž˜ : ${this._power}`;
      }
      set power(power) {
        this._power = power; // ์‹ค์ œ ํ•„๋“œ๋ช… _power์— ๊ฐ’ ์ €์žฅ
      }
    }
    
    
    const p1 = new Player('Kassid', '์ „์‚ฌ');

     

    getterํ•จ์ˆ˜์™€ setterํ•จ์ˆ˜์— "_"๋ฅผ ๋ถ™์ธ _power๋ฅผ ์ œ์™ธํ•˜๊ณ  ์˜ค๋ฅ˜ ์˜ˆ์‹œ์™€ ๋ชจ๋‘ ๋™์ผํ•˜๋‹ค.

     

     

    ์›๋ฆฌ

    constructor์—์„œ setterํ•จ์ˆ˜์— ๋ฐ›์•„์˜จ argument๋ฅผ ํ• ๋‹นํ•˜๊ณ ,

    setterํ•จ์ˆ˜ ๋‚ด์—์„œ ์ •์˜๋œ ์‹ค์ œ ํ•„๋“œ๋ช… "_power"์— ๊ฐ’์„ ์ €์žฅํ•˜๋Š” ์›๋ฆฌ์ด๋‹ค. (๋‹น์—ฐํžˆ ์ž„์˜๋กœ ์ž‘์„ฑ๋œ ํ•„๋“œ๋ช…์ž„)

    ์‹ค์ œ ํ•„๋“œ๋ช… _power์— ๊ฐ’์ด ์ €์žฅ๋œ ๋ชจ์Šต

    ์ด๋Ÿฌํ•œ ๋ฐฉ์‹์œผ๋กœ ์ž๊ธฐ๋ฐ˜๋ณตํ˜ธ์ถœ์„ ๋ฐฉ์ง€ํ•˜๊ณ 

    ์‚ฌ์šฉ์ž์˜ ์ž…์žฅ์—์„œ setterํ•จ์ˆ˜์™€ ํ•„๋“œ๋ช…์„ ๋™์ผํ•˜๊ฒŒ ์ด์šฉํ•˜๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

     

     

     


    2. ์€๋‹‰

    ๊ฐ์ฒด์ง€ํ–ฅ์˜ ์ฃผ์š” ์š”์†Œ ์ค‘ ํ•˜๋‚˜๋Š” '์€๋‹‰'์ด๋‹ค.

    ๊ฐ์ฒด ๋‚ด๋ถ€์˜ ๊ฐ’์„ ๊ฐ์ถ˜๋‹ค๋Š” ์˜๋ฏธ์ด๊ณ 

    ์ธ์Šคํ„ด์Šค์˜ ํ”„๋กœํผํ‹ฐ ๊ฐ’์„ ํ•จ๋ถ€๋กœ ์—ด๋žŒํ•˜๊ฑฐ๋‚˜ ์กฐ์ž‘ํ•˜์ง€ ๋ชปํ•˜๋„๋ก ํ•˜๊ธฐ ์œ„ํ•œ ์˜๋„์ด๋‹ค.

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

     

    ์ด๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด์„œ ๋‹ค๋ฅธ ์–ธ์–ด์—๋„ ์žˆ๋Š” ๊ฐœ๋…์ธ private๋ฅผ ํ™œ์šฉํ•˜๋ฉด ํ•„๋“œ๋ฅผ ์€๋‹‰ํ•  ์ˆ˜ ์žˆ๋‹ค.

     

    privateํ•„๋“œ

    (ํ˜„์žฌ private๋Š” ์•„์ง ๊ณต์‹์ ์œผ๋กœ ํ™œ์šฉ๋˜์ง€ ์•Š๊ธฐ๋•Œ๋ฌธ์— ์ผ๋ถ€ ๋ธŒ๋ผ์šฐ์ €์—์„œ๋Š” ์ผ๋ถ€ ๊ธฐ๋Šฅ์ด ์ œ๋Œ€๋กœ ์ˆ˜ํ–‰๋˜์ง€ ์•Š๋Š”๋‹คใ…  )

     

    ํ•„๋“œ ๋ช…์˜ ์•ž์— '#' ์„ ๋ถ™์ผ ๊ฒฝ์šฐ privateํ•„๋“œ์— ์ •์˜๋œ๋‹ค.

    ๋˜ํ•œ constructor ๋‚ด๋ถ€๊ฐ€ ์•„๋‹Œ ํด๋ž˜์Šค ๋ฐ”๋กœ ์•ˆ์ชฝ์— ์ •์˜ํ•ด์•ผํ•œ๋‹ค.

    class Student {
      #name = '';
      #idNum = 0;
      constructor (name, idNum){
        this.#name = name;
        this.#idNum = idNum;
      }
    }
    
    const student1 = new Student('Kassid', 25);

     

    console.log(student1.#name);
    
    >>> SyntaxError: Cannot reference undeclared private names: "#name"

    ์‚ฌํŒŒ๋ฆฌ ํ™˜๊ฒฝ์—์„œ ํ™•์ธ

    (ํฌ๋กฌ์—์„œ๋Š” ์ž‘์„ฑ์ผ ๊ธฐ์ค€ ์ ‘๊ทผ ๊ฐ€๋Šฅ, 2023.4.10)

     

    console.log(student1['#name']);
    
    >>> undefined

     

    ์ด์ฒ˜๋Ÿผ ํด๋ž˜์Šค ์™ธ๋ถ€์—์„œ private ํ•„๋“œ์— ์ ‘๊ทผํ•˜๋Š” ๊ฒƒ์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค.

    ๋”ฐ๋ผ์„œ ํ•ด๋‹น ํ•„๋“œ์— ๋Œ€ํ•œ ์กฐ์ž‘์€ ํด๋ž˜์Šค ๋‚ด๋ถ€์—์„œ ์ด๋ฃจ์–ด์ ธ์•ผํ•œ๋‹ค.

     

    ํด๋ž˜์Šค ๋‚ด๋ถ€์—์„œ์˜ privateํ•„๋“œ ์ ‘๊ทผ

    class Student {
      #name = '';
      #idNum = 0;
      constructor (name, idNum){
        this.#name = name;
        this.#idNum = idNum;
      }
      
      get info (){
        return `์ด๋ฆ„ : ${this.#name} ๋ฒˆํ˜ธ : ${this.#idNum}`
      }
      get idNum (){
        return this.#idNum;
      }
      set idNum (idNum){
        if (typeof idNum === 'number' && idNum > 0){
            this.#idNum = idNum;
        }
      }
    }
    
    const student1 = new Student('Kassid', 25);

     

    student1.idNum = 10;
    console.log(student1);
    
    >>> Student {#name: 'Kassid', #idNum: 10}

     

    constructor๋‚˜ ์ ‘๊ทผ์ž ํ”„๋กœํผํ‹ฐ, ์ผ๋ฐ˜ ํ•จ์ˆ˜ ๋“ฑ

    ํด๋ž˜์Šค ๋‚ด๋ถ€์—์„œ๋Š” private ํ•„๋“œ์— ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

     

    ์ด๋Ÿฌํ•œ ๋ฐฉ์‹์œผ๋กœ ์ธ์Šคํ„ด์Šค์˜ ๊ฐ’์— ๋ฐ”๋กœ ์ ‘๊ทผํ•˜๋Š” ๊ฒƒ์„ ๋ง‰๊ธฐ ์œ„ํ•ด์„œ ์ด์ฒ˜๋Ÿผ ์บก์Šํ™”๋ฅผ ํ•˜๋ฉด๋œ๋‹ค!

    ๋Œ“๊ธ€