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; }
}
> ๋ง์ฝ, ๋ฐ์ดํฐ ๊ตฌ์กฐ๋ฅผ ๋ถ๋ณ์ผ๋ก ๋ง๋ค ์ ์๋ ํ๋ก๊ทธ๋๋ฐ ์ธ์ด๋ฅผ ์ฌ์ฉํ๋ค๋ฉด
์บก์ํํ๋ ๋์ ๋ฐ์ดํฐ ๊ตฌ์กฐ์ ๊ฐ์ ๋ณต์ ํด ์๋ก์ด ์ด๋ฆ์ผ๋ก ์ ์ธํ๋ค.
๊ทธ๋ฐ ๋ค์ ์ฌ์ฉํ๋ ๊ณณ์ ์ฐพ์ ํ๋์ฉ ์ ๋ฐ์ดํฐ๋ฅผ ์ฌ์ฉํ๋๋ก ์์ ํ๊ณ , ๋ง์ง๋ง์ผ๋ก ์๋์ ๋ฐ์ดํฐ ๊ตฌ์กฐ๋ฅผ ์ ๊ฑฐํ๋ฉด ๋๋ค. ๊ฐ๋ณ ๋ฐ์ดํฐ ๊ตฌ์กฐ๋ฅผ ์ด์ฉํ๋ค๋ฉด ๋ฐ์ดํฐ๋ฅผ ๋ณต์ ํ๋ ํ์๊ฐ ์ฌ์์ผ๋ก ์ด์ด์ง ์ ์๋ค.
| 6.6. ๋ณ์ ์บก์ํํ๊ธฐ (Encapsulate Variable) (0) | 2021.09.26 |
|---|---|
| 6.3. ๋ณ์ ์ถ์ถํ๊ธฐ (0) | 2021.09.26 |
| 6.1. ํจ์ ์ถ์ถํ๊ธฐ (0) | 2021.09.26 |
| 6.7. ๋ณ์ ์ด๋ฆ ๋ฐ๊พธ๊ธฐ (Rename Variable) (0) | 2021.09.25 |
| 6.2. ํจ์ ์ธ๋ผ์ธํ๊ธฐ (inline Function) (0) | 2021.09.25 |