Immutable Objects

728x90

Object.isExtensible()

- 변수를 추가할 수 있는지 설정하는 옵션 (True : 변수 추가 가능)

const company = {
    name: 'planb',
    year: 1988,

    get age(){
        return new Date().getFullYear() - this.year;
    },

    set age(age){
        this.year = new Date().getFullYear() - age;
    }
}

console.log(company);
console.log(Object.isExtensible(company));

company['industry'] = 's/w';

console.log(company);

output

Object.preventExtensions()

- 변수를 추가할 수 없도록 설정하는 함수, 오류는 발생하지 않으나 변수가 추가되지 않음

const company = {
    name: 'planb',
    year: 1988,

    get age(){
        return new Date().getFullYear() - this.year;
    },

    set age(age){
        this.year = new Date().getFullYear() - age;
    }
}

Object.preventExtensions(company);

console.log(Object.isExtensible(company));

company['employee'] = 4;
console.log(company);

output

delete

- 프로퍼티 삭제

const company = {
    name: 'planb',
    year: 1988,

    get age(){
        return new Date().getFullYear() - this.year;
    },

    set age(age){
        this.year = new Date().getFullYear() - age;
    }
}
company['industry'] = 's/w';
console.log(company);

delete company['industry'];
console.log(company);

output

Seal

- true : 프로퍼티 추가 불가
- false : 프로퍼티 추가 가능

const company = {
    name: 'planb',
    year: 1988,

    get age(){
        return new Date().getFullYear() - this.year;
    },

    set age(age){
        this.year = new Date().getFullYear() - age;
    }
}
console.log(company);
console.log(Object.isSealed(company));
Object.seal(company);
console.log(Object.isSealed(company));

company['employee'] = 4;
console.log(company);

output

delete

- seal : true인 경우 delete 적용 안됨, 오류 발생 안함

const company = {
    name: 'planb',
    year: 1988,

    get age(){
        return new Date().getFullYear() - this.year;
    },

    set age(age){
        this.year = new Date().getFullYear() - age;
    }
}

Object.seal(company);
console.log(Object.isSealed(company));

company['employee'] = 4;
console.log(company);

delete company['name'];
console.log(company);

output

defineProperty()

- seal로 봉인하였으나 value 값 변경 가능함, configurable이 false인 상황가 동일함

const company = {
    name: 'planb',
    year: 1988,

    get age(){
        return new Date().getFullYear() - this.year;
    },

    set age(age){
        this.year = new Date().getFullYear() - age;
    }
}

Object.seal(company);
console.log(Object.isSealed(company));

company['employee'] = 4;
console.log(company);

delete company['name'];
console.log(company);

Object.defineProperty(company, 'name', {
    value: 'planc'
});
console.log(Object.getOwnPropertyDescriptor(company, 'name'));

output

Fleezed

- 읽이 외에 모든 기능을 불가능하게 함.

const company = {
    name: 'planb',
    year: 1988,

    get age(){
        return new Date().getFullYear() - this.year;
    },

    set age(age){
        this.year = new Date().getFullYear() - age;
    }
}

/**
 * Freezed
 * 
 * 읽기 외에 모든 기능을 불가능하게 만든다.
 */
console.log(Object.isFrozen(company));

Object.freeze(company);
console.log(Object.isFrozen(company));

company['employee'] = 4;
console.log(company);

delete company['name'];
console.log(company);

output

- delete로 프로퍼티 삭제 불가

defineProperty()

- value 값 변경하려고 시도하였으나 오류 발생

Object.defineProperty(company, 'name', {
    value: 'planc',
})
console.log(Object.getOwnPropertyDescriptor(company, 'name'));

output

종속 객체의 freezed

- 상위 객체를 freezed 하여도 종속 객체는 freezed되지 않음

const company2 = {
    name: 'planb',
    year: 2003,
    company3: {
        name: 'planc',
        year: 2002,
    },
};
Object.freeze(company2);

console.log(Object.isFrozen(company2));
console.log(Object.isFrozen(company2['company3']));

output

728x90

'JavaScript' 카테고리의 다른 글

Prototype 프로토타입  (0) 2025.03.10
함수를 이용한 객체 생성  (2) 2025.03.01
Property Attribute  (0) 2025.03.01
객체 OOP 생성 방법  (0) 2025.03.01
super & override  (0) 2025.03.01