객체의 값이 변하지 않게 막아준다.
let obj1 = {
name: 'ori-name'
};
Object.freeze(obj1);
let obj2 = obj1;
obj1.name = 'newName';
obj2.name = 'newName';
console.log(obj1); /** obj1.name: ori-name */
console.log(obj2); /** obj2.name: ori-name */
(참고: const는 객체를 담고있는 변수 obj1에 다른 값, 객체를 재할당 하는것을 막아주는 것이다. )
주의! Object.assign()과 마찬가지로, 객체 내부에 중첩된 객체가 존재할 경우,
그 내부 객체는 freeze()되지 않는다.
let obj1 = {
name: 'ori-name',
arr: [100, 200]
};
Object.freeze(obj1);
obj1.arr.push(-1);
console.log(obj1.arr); /** obj1.arr = [100, 200, -1] -> freeze되지 않는다. */
'Dev- > java script, jQuery, Ajax' 카테고리의 다른 글
자바스크립트 내부 객체를 변수에 할당 (0) | 2019.08.29 |
---|---|
자바스크립트 typeof() vs instanceof() 차이 (0) | 2019.08.18 |
자바스크립트 중첩된 객체의 복사 (0) | 2019.08.15 |
자바스크립트 Object.create vs Object.assign 차이 (0) | 2019.08.14 |
자바스크립트 hasOwnProperty (0) | 2019.08.14 |