computed properties
์ ๊ทผ
noran.name
noran['name']; // computed properties
['name'] ํํ๋ก ๋ฐ์์ค๋ key๋ stringํ์ ์ด์ด์ผ ํ๋ค.
key ์ถ๊ฐ
noran['hasjob'] = true;
์ด๋จ๋ computed properties๋ฅผ ์ฐ๊ณ /์ฐ์ง ์์๊น?
- noran.name ํํ๋ ์ฝ๋ฉํ๋ ์๊ฐ ์ค์๊ฐ์ผ๋ก key์ ํด๋นํ๋ ๊ฐ์ ๋ฐ์์ค๊ณ ์ถ์ ๋ ์ด๋ค.
- noran['name'] ํํ๋ runtime์์ ๊ฒฐ์ ๋๋ key (์ด๋ค key๋ฅผ ๋ฐ์์ฌ์ง ๋ชจ๋ฅผ ๋)์ผ ๋ ์ด๋ค.
computed properties๋ฅผ ์ฐ๋ ์ํฉ ์์
function Goodcoder(obj, key) {
console.log(obj.key);
}
printValue(noran, 'name'); //undefined
์ Goodcoderํจ์๋ ํจ์๋ฅผ ์์ฑํ ๋๋ key์ ์ด๋ค ํํ๊ฐ ๋ค์ด๊ฐ ์ง ๋ชจ๋ฅธ๋ค. ๊ทธ๋ฌ๋ฏ๋ก computed properties ํํ๋ก ์ฌ์ฉํ๋ค.
function Goodcoder(obj, key) {
console.log(obj['key']); //computed properties
}
printValue(noran, 'name'); //20
์ถ์ฒ : ๋๋ฆผ์ฝ๋ฉ https://www.youtube.com/@dream-coding / MDN https://developer.mozilla.org/ko