์ƒ์„ธ ์ปจํ…์ธ 

๋ณธ๋ฌธ ์ œ๋ชฉ

9.2. ํ•„๋“œ ์ด๋ฆ„ ๋ฐ”๊พธ๊ธฐ (Rename Field)

๐Ÿœ๊ฐœ๋ฐœ์ž๋ผ๋ฉด/์ฑ…์„ ์ฝ์žโœ

by :ํ•ดํ”ผ๋ž˜๋น—๐Ÿพ 2021. 9. 25. 14:24

๋ณธ๋ฌธ

- ๋ ˆ์ฝ”๋“œ ๊ตฌ์กฐ์ฒด์˜ ํ•„๋“œ ์ด๋ฆ„

- ํด๋ž˜์Šค ๋‚ด ํ•„๋“œ

- ํด๋ž˜์Šค ๋‚ด ์‚ฌ์šฉํ•˜๋Š” ๋ถ€๋ถ„, getter, setter๋ฉ”์„œ๋“œ 

 

 


๋ฐฉ๋ฒ•

1. ๋ ˆ์ฝ”๋“œ์˜ ์œ ํšจ ๋ฒ”์œ„๊ฐ€ ์ œํ•œ์ ์ด๋ผ๋ฉด ํ•„๋“œ์— ์ ‘๊ทผํ•˜๋Š” ๋ชจ๋“  ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•œ ํ›„ ํ…Œ์ŠคํŠธํ•œ๋‹ค. ์ดํ›„ ๋‹จ๊ณ„๋Š” ํ•„์š”์—†๋‹ค.

2. ๋ ˆ์ฝ”๋“œ๊ฐ€ ์บก์Аํ™”๋˜์ง€ ์•Š์•˜๋‹ค๋ฉด ์šฐ์„  ๋ ˆ์ฝ”๋“œ๋ฅผ ์บก์Аํ™”ํ•œ๋‹ค.

3. ์บก์Аํ™”๋œ ๊ฐ์ฒด ์•ˆ์˜ private ํ•„๋“œ๋ช…์„ ๋ณ€๊ฒฝํ•˜๊ณ , ๊ทธ์— ๋งž๊ฒŒ ๋‚ด๋ถ€ ๋ฉ”์„œ๋“œ๋“ค์„ ์ˆ˜์ •ํ•œ๋‹ค.

4. ํ…Œ์ŠคํŠธํ•œ๋‹ค.

5. ์ƒ์„ฑ์ž์˜ ๋งค๊ฐœ๋ณ€์ˆ˜ ์ค‘ ํ•„๋“œ์™€ ์ด๋ฆ„์ด ๊ฒน์น˜๋Š” ๊ฒŒ ์žˆ๋‹ค๋ฉด ํ•จ์ˆ˜ ์„ ์–ธ ๋ฐ”๊พธ๊ธฐ๋กœ ๋ณ€๊ฒฝํ•œ๋‹ค.

6. ์ ‘๊ทผ์ž๋“ค์˜ ์ด๋ฆ„๋„ ๋ฐ”๊ฟ”์ค€๋‹ค. (6.5์ ˆ)

 

 


  ํ•„๋“œ ์ด๋ฆ„ ๋ฐ”๊พธ๊ธฐ  

const organization = {name : "์‚ผ์„ฑ", country: "KOR"};
name์„ title๋กœ ๋ฐ”๊พธ๊ณ  ์‹ถ๋‹ค.
organization์€ ๋„๋ฆฌ ์ฐธ์กฐ๋˜๋Š” ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ์ด๋‹ค.

 

// ๋ณ€๊ฒฝ ์ด์ „
const organization = {name: "์‚ผ์„ฑ", country : "KOR"}

// 1) ์บก์Аํ™”
class Organization {
    constructor(data){
        this._name = data.name;   // _name๊ณผ name์„ ๋ถ„๋ฆฌ์‹œ์ผฐ๋‹ค.
        this._country = data.country;
    }
    get name(){return this._name;}
    set name(aString){this._name = aString;}
    get country() {return this._country;}
    set country(aCountryCode){this._country=aCountryCode;}
}
const organization = new Organization({name:"์‚ผ์„ฑ",country:"KOR"});


// 2) ๋ณ„๋„์˜ ํ•„๋“œ๋ฅผ ์ •์˜ํ•˜๊ณ  ์ƒ์„ฑ์ž์™€ ์ ‘๊ทผ์ž์—์„œ ๋‘˜์„ ๊ตฌ๋ถ„ํ•ด(name,_title) ์‚ฌ์šฉํ•˜๋„๋ก ํ•œ๋‹ค.
class Organization {
    constructor(data){
        this._title = data.name; // ์ƒ์„ฑ์ž
        this._country = data.country;
    }
    get name(){ return this._title; } //์ ‘๊ทผ์ž
    set name(aString){ this._title = aString; }
    get country(){ return this._country; }
    set country(aCountryCode) { this._country = aCountryCode; }
}

// 3) data.title๋กœ๋„ ๋ฐ›์•„๋“ค์ผ ์ˆ˜ ์žˆ๋„๋ก ํ•œ๋‹ค
// {title:"์‚ผ์„ฑ",country:"KOR"}, {name:"์‚ผ์„ฑ",country:"KOR"} ๋‘˜ ๋‹ค ๊ฐ€๋Šฅํ•˜๊ฒŒ
class Organization {
    constructor(data){
        this._title = (data.title!==undefined)?data.title:data.name; //*
        this._country = data.country;
    }
    get name(){ return this._title; }
    set name(aString){ this._title = aString; }
    get country(){ return this._country; }
    set country(aCountryCode) { this._country = aCountryCode; }
}

// 4) ์ƒ์„ฑ์ž์—์„œ data.title๋กœ๋งŒ ๋ฐ›์•„๋“ค์ผ ์ˆ˜ ์žˆ๊ฒŒ ๋ฐ”๊พผ๋‹ค.
class Organization {
    constructor(data){
        this._title = data.title; //*
        this._country = data.country;
    }
    get name(){ return this._title; }
    set name(aString){ this._title = aString; }
    get country(){ return this._country; }
    set country(aCountryCode) { this._country = aCountryCode; }
}

//5) getter,setter์ด๋ฆ„๋„ ๋ณ€๊ฒฝํ•ด์ค€๋‹ค.
class Organization {
    constructor(data){
        this._title = data.title; //*
        this._country = data.country;
    }
    get title(){ return this._title; }
    set title(aString){ this._title = aString; }
    get country(){ return this._country; }
    set country(aCountryCode) { this._country = aCountryCode; }
}

> ๋งŒ์•ฝ, ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ๋ฅผ ๋ถˆ๋ณ€์œผ๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด

์บก์Аํ™”ํ•˜๋Š” ๋Œ€์‹  ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ์˜ ๊ฐ’์„ ๋ณต์ œํ•ด ์ƒˆ๋กœ์šด ์ด๋ฆ„์œผ๋กœ ์„ ์–ธํ•œ๋‹ค.

๊ทธ๋Ÿฐ ๋‹ค์Œ ์‚ฌ์šฉํ•˜๋Š” ๊ณณ์„ ์ฐพ์•„ ํ•˜๋‚˜์”ฉ ์ƒˆ ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜๋„๋ก ์ˆ˜์ •ํ•˜๊ณ , ๋งˆ์ง€๋ง‰์œผ๋กœ ์›๋ž˜์˜ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ๋ฅผ ์ œ๊ฑฐํ•˜๋ฉด ๋œ๋‹ค. ๊ฐ€๋ณ€ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ๋ฅผ ์ด์šฉํ•œ๋‹ค๋ฉด ๋ฐ์ดํ„ฐ๋ฅผ ๋ณต์ œํ•˜๋Š” ํ–‰์œ„๊ฐ€ ์žฌ์•™์œผ๋กœ ์ด์–ด์งˆ ์ˆ˜ ์žˆ๋‹ค. 

 

 

 

 

728x90

๊ด€๋ จ๊ธ€ ๋”๋ณด๊ธฐ