Chapter16 ํ๋กํผํฐ ์ดํธ๋ฆฌ๋ทฐํธ
๋ค์ ๋ฌธ์ฅ์ true or false๋ฅผ ํ๋จํ์ธ์.
(1) ์กด์ฌํ์ง ์๋ ํ๋กํผํฐ๋ ์์๋ฐ์ ํ๋กํผํฐ์ ๋ํ ํ๋กํผํฐ ๋์คํฌ๋ฆฝํฐ๋ฅผ ์๊ตฌํ๋ฉด ์๋ฌ๊ฐ ๋ฐ์ํ๋ค.
(2) ์ ๊ทผ์ ํ๋กํผํฐ๋ ์์ฒด์ ์ผ๋ก ๊ฐ(ํ๋กํผํฐ ์ดํธ๋ฆฌ๋ทฐํธ [[Value]])์ ๊ฐ์ง๋ค.
(3) [[Configurable]]์ ๊ฐ์ด false์ธ ๊ฒฝ์ฐ ๊ฐ์ ๋ณ๊ฒฝํ๋ฉด ์๋ฌ ์์ด ๋ฌด์ํ๋ค.
๋ค์ ์ฝ๋์ ์คํ ๊ฒฐ๊ณผ๋ฅผ ์์ธกํ์ธ์.
const person = { name : "Jing" } ;
Object . freeze ( person ) ;
console . log ( Object . getOwnPropertyDescriptors ( person ) ) ; // (1)
person . age = 23 ;
console . log ( person ) ; // (2)
delete person . name ;
console . log ( person ) ; // (3)
person . name = "JiEun" ;
console . log ( person ) ; // (4)
Object . defineProperty ( person , "name" , { configurable : true } ) ; // (5)
๋ค์ ์คํ ๊ฒฐ๊ณผ๋ฅผ ์ฐ์์ค
const obj = {
a : 1 ,
b : {
c : 2
}
}
Object . freeze ( obj ) ;
const descriptor = Object . getOwnPropertyDescriptor ( obj , b ) ;
console . log ( descriptor ) ;
obj . a = 2 ;
obj . b . c = 3 ;
console . log ( obj ) ;
๊ฐ์ฒด ๋ฆฌํฐ๋ด ์ ์ธ๋ฐฉ์(1)๊ณผ defineProperty๋ฐฉ์(2)์ผ๋ก ์ ์ธํ์๋ ์ดํธ๋ฆฌ๋ทฐํธ(writable, enumerable,configurable) ์ ์์ ์ฐจ์ด์ ์ด ๋ฌด์์ธ์ง ์์ฑํ์์ค.
์ถ๋ ฅ๊ฒฐ๊ณผ๋ฅผ ์์ธกํ์ธ์
const obj = { } ;
Object . defineProperties ( obj , {
firstName : {
value : "deep" ,
writable : true ,
enumerable : true ,
configurable : false ,
} ,
lastName : {
value : "dive" ,
writable : true ,
enumerable : true ,
configurable : false ,
} ,
} ) ;
Object . defineProperties ( obj , {
firstName : {
writable : false ,
} ,
} ) ;
console . log ( Object . getOwnPropertyDescriptors ( obj ) ) ; // (1)
Object . defineProperties ( obj , {
lastName : {
writable : false ,
enumerable : false ,
} ,
} ) ;
console . log ( Object . getOwnPropertyDescriptors ( obj ) ) ; // (2)
ํ๋กํผํฐ ์ดํธ๋ฆฌ๋ทฐํธ์ ์ข
๋ฅ๋ก Value
, Writable
, Enumerable
, Configurable
, Get
, Set
์ด ์์ต๋๋ค. ๊ฐ๊ฐ์ ๋ํด ๊ฐ๋จํ ์ค๋ช
ํ์์ค.
Value :
Writable :
Enumerable :
Configurable :
Get :
Set :