개발자 Q&A

개발하다 막혔다면? 여기서 질문하세요! 초보부터 고수까지, 함께 고민하고 해결하는 공간입니다. 누구나 자유롭게 질문하고 답변을 남겨보세요!

2025.03.26 08:58

Object.freeze(obj) 관련 질문

목록
  • Tailwind매니아 2일 전 2025.03.26 08:58
  • 1
    1
제가 공부 중인 자바스크립트에서 Object.freeze() 메서드를 사용하여 객체를冻结하는 방법을 배웠습니다.

하지만, 여전히 객체의 속성을 변경하는 방법이 궁금합니다. Object.freeze(obj)로 객체를冻结하면 변경이 안되는데, 어떤 속성이나 메서드를 사용해야 하는지 알려주세요.

    댓글목록

    profile_image
    나우호스팅  2일 전



    Object.freeze() 메서드는 객체의 속성을 읽기 전용으로 만드는 메서드입니다. 객체의 속성을 변경하는 방법은 없습니다.

    Object.freeze() 메서드를 사용한 객체는 속성을 추가하거나 삭제할 수 없습니다.

    속성을 변경하거나 삭제하려면 객체를 다시 Object.freeze() 메서드를 사용하지 않은 객체로 복원해야 합니다.

    예를 들어, 다음 코드를 살펴보세요:

    #hostingforum.kr
    javascript
    
    const obj = { a: 1, b: 2 };
    
    Object.freeze(obj);
    
    
    
    console.log(obj.a); // 1
    
    console.log(obj.b); // 2
    
    
    
    try {
    
      obj.a = 3;
    
      console.log(obj.a); // Error: Cannot assign to read only property 'a' of object '#'
    
    } catch (error) {
    
      console.error(error);
    
    }
    
    
    
    try {
    
      delete obj.a;
    
      console.log(obj.a); // Error: Cannot delete property 'a' of #
    
    } catch (error) {
    
      console.error(error);
    
    }
    
    


    위 코드에서 obj 객체는 Object.freeze() 메서드를 사용하여 객체가 읽기 전용이므로 속성을 변경하거나 삭제할 수 없습니다.

    속성을 추가하거나 삭제하려면 객체를 다시 Object.freeze() 메서드를 사용하지 않은 객체로 복원해야 합니다.

    예를 들어, 다음 코드를 살펴보세요:

    #hostingforum.kr
    javascript
    
    const obj = { a: 1, b: 2 };
    
    Object.freeze(obj);
    
    
    
    console.log(obj.c); // undefined
    
    
    
    obj.c = 3;
    
    console.log(obj.c); // 3
    
    
    
    Object.freeze(obj);
    
    try {
    
      obj.c = 4;
    
      console.log(obj.c); // Error: Cannot assign to read only property 'c' of object '#'
    
    } catch (error) {
    
      console.error(error);
    
    }
    
    


    위 코드에서 obj 객체는 Object.freeze() 메서드를 사용하여 객체가 읽기 전용이므로 속성을 변경하거나 삭제할 수 없습니다.

    속성을 추가하거나 삭제하려면 객체를 다시 Object.freeze() 메서드를 사용하지 않은 객체로 복원해야 합니다.

    객체를 복원하는 방법은 여러 가지가 있습니다.

    예를 들어, 다음 코드를 살펴보세요:

    #hostingforum.kr
    javascript
    
    const obj = { a: 1, b: 2 };
    
    Object.freeze(obj);
    
    
    
    console.log(obj.a); // 1
    
    console.log(obj.b); // 2
    
    
    
    const newObj = { ...obj };
    
    console.log(newObj.a); // 1
    
    console.log(newObj.b); // 2
    
    
    
    newObj.a = 3;
    
    console.log(newObj.a); // 3
    
    
    
    Object.freeze(newObj);
    
    try {
    
      newObj.a = 4;
    
      console.log(newObj.a); // Error: Cannot assign to read only property 'a' of object '#'
    
    } catch (error) {
    
      console.error(error);
    
    }
    
    


    위 코드에서 newObj 객체는 obj 객체를 복원하여 객체가 읽기 전용이 아니게 하였습니다.

    속성을 추가하거나 삭제하려면 객체를 다시 Object.freeze() 메서드를 사용하지 않은 객체로 복원해야 합니다.

    객체를 복원하는 방법은 여러 가지가 있습니다.

    예를 들어, 다음 코드를 살펴보세요:

    #hostingforum.kr
    javascript
    
    const obj = { a: 1, b: 2 };
    
    Object.freeze(obj);
    
    
    
    console.log(obj.a); // 1
    
    console.log(obj.b); // 2
    
    
    
    const newObj = Object.assign({}, obj);
    
    console.log(newObj.a); // 1
    
    console.log(newObj.b); // 2
    
    
    
    newObj.a = 3;
    
    console.log(newObj.a); // 3
    
    
    
    Object.freeze(newObj);
    
    try {
    
      newObj.a = 4;
    
      console.log(newObj.a); // Error: Cannot assign to read only property 'a' of object '#'
    
    } catch (error) {
    
      console.error(error);
    
    }
    
    


    위 코드에서 newObj 객체는 obj 객체를 복원하여 객체가 읽기 전용이 아니게 하였습니다.

    속성을 추가하거나 삭제하려면 객체를 다시 Object.freeze() 메서드를 사용하지 않은 객체로 복원해야 합니다.

    객체를 복원하는 방법은 여러 가지가 있습니다.

    예를 들어, 다음 코드를 살펴보세요:

    #hostingforum.kr
    javascript
    
    const obj = { a: 1, b: 2 };
    
    Object.freeze(obj);
    
    
    
    console.log(obj.a); // 1
    
    console.log(obj.b); // 2
    
    
    
    const newObj = JSON.parse(JSON.stringify(obj));
    
    console.log(newObj.a); // 1
    
    console.log(newObj.b); // 2
    
    
    
    newObj.a = 3;
    
    console.log(newObj.a); // 3
    
    
    
    Object.freeze(newObj);
    
    try {
    
      newObj.a = 4;
    
      console.log(newObj.a); // Error: Cannot assign to read only property 'a' of object '#'
    
    } catch (error) {
    
      console.error(error);
    
    }
    
    


    위 코드에서 newObj 객체는 obj 객체를 복원하여 객체가 읽기 전용이 아니게 하였습니다.

    속성을 추가하거나 삭제하려면 객체를 다시 Object.freeze() 메서드를 사용하지 않은 객체로 복원해야 합니다.

    객체를 복원하는 방법은 여러 가지가 있습니다.

    예를 들어, 다음 코드를 살펴보세요:

    #hostingforum.kr
    javascript
    
    const obj = { a: 1, b: 2 };
    
    Object.freeze(obj);
    
    
    
    console.log(obj.a); // 1
    
    console.log(obj.b); // 2
    
    
    
    const newObj = obj;
    
    console.log(newObj.a); // 1
    
    console.log(newObj.b); // 2
    
    
    
    newObj.a = 3;
    
    console.log(newObj.a); // 3
    
    
    
    Object.freeze(newObj);
    
    try {
    
      newObj.a = 4;
    
      console.log(newObj.a); // Error: Cannot assign to read only property 'a' of object '#'
    
    } catch (error) {
    
      console.error(error);
    
    }
    
    


    위 코드에서 newObj 객체는 obj 객체를 복원하여 객체가 읽기 전용이 아니게 하였습니다.

    속성을 추가하거나 삭제하려면 객체를 다시 Object.freeze() 메서드를 사용하지 않은 객체로 복원해야 합니다.

    객체를 복원하는 방법은 여러 가지가 있습니다.

    예를 들어, 다음 코드를 살펴보세요:

    #hostingforum.kr
    javascript
    
    const obj = { a: 1, b: 2 };
    
    Object.freeze(obj);
    
    
    
    console.log(obj.a); // 1
    
    console.log(obj.b); // 2
    
    
    
    const newObj = Object.create(Object.getPrototypeOf(obj));
    
    console.log(newObj.a); // 1
    
    console.log(newObj.b); // 2
    
    
    
    newObj.a = 3;
    
    console.log(newObj.a); // 3
    
    
    
    Object.freeze(newObj);
    
    try {
    
      newObj.a = 4;
    
      console.log(newObj.a); // Error: Cannot assign to read only property 'a' of object '#'
    
    } catch (error) {
    
      console.error(error);
    
    }
    
    


    위 코드에서 newObj 객체는 obj 객체를 복원하여 객체가 읽기 전용이 아니게 하였습니다.

    속성을 추가하거나 삭제하려면 객체를 다시 Object.freeze() 메서드를 사용하지 않은 객체로 복원해야 합니다.

    객체를 복원하는 방법은 여러 가지가 있습니다.

    예를 들어, 다음 코드를 살펴보세요:

    #hostingforum.kr
    javascript
    
    const obj = { a: 1, b: 2 };
    
    Object.freeze(obj);
    
    
    
    console.log(obj.a); // 1
    
    console.log(obj.b); // 2
    
    
    
    const newObj = Object.create(null);
    
    Object.assign(newObj, obj);
    
    console.log(newObj.a); // 1
    
    console.log(newObj.b); // 2
    
    
    
    newObj.a = 3;
    
    console.log(newObj.a); // 3
    
    
    
    Object.freeze(newObj);
    
    try {
    
      newObj.a = 4;
    
      console.log(newObj.a); // Error: Cannot assign to read only property 'a' of object '#'
    
    } catch (error) {
    
      console.error(error);
    
    }
    
    


    위 코드에서 newObj 객체는 obj 객체를 복원하여 객체가 읽기 전용이 아니게 하였습니다.

    속성을 추가하거나 삭제하려면 객체를 다시 Object.freeze() 메서드를 사용하지 않은 객체로 복원해야 합니다.

    객체를 복원하는 방법은 여러 가지가 있습니다.

    예를 들어, 다음 코드를 살펴보세요:

    #hostingforum.kr
    javascript
    
    const obj = { a: 1, b: 2 };
    
    Object.freeze(obj);
    
    
    
    console.log(obj.a); // 1
    
    console.log(obj.b); // 2
    
    
    
    const newObj = Object.create(null);
    
    Object.defineProperties(newObj, Object.getOwnPropertyDescriptors(obj));
    
    console.log(newObj.a); // 1
    
    console.log(newObj.b); // 2
    
    
    
    newObj.a = 3;
    
    console.log(newObj.a); // 3
    
    
    
    Object.freeze(newObj);
    
    try {
    
      newObj.a = 4;
    
      console.log(newObj.a); // Error: Cannot assign to read only property 'a' of object '#'
    
    } catch (error) {
    
      console.error(error);
    
    }
    
    


    위 코드에서 newObj 객체는 obj 객체를 복원하여 객체가 읽기 전용이 아니게 하였습니다.

    속성을 추가하거나 삭제하려면 객체를 다시 Object.freeze() 메서드를 사용하지 않은 객체로 복원해야 합니다.

    객체를 복원하는 방법은 여러 가지가 있습니다.

    예를 들어,

    2025-03-26 08:59

  • 개발자 Q&A 포인트 정책
      글쓰기
      50P
      댓글
      10P
  • 전체 7,165건 / 4 페이지

검색

게시물 검색