Object.freeze(obj) devuelve el error

El ejercicio pide congelar una propiedad dentro de un objeto y JS devuelve el error [TypeError: Cannot assign to read only property ‘PI’ of object ‘#’]

A pesar de que sí congela el objeto

  **Tu código hasta el momento**

function freezeObj() {
const MATH_CONSTANTS = {
  PI: 3.14
};
// Cambia solo el código debajo de esta línea

Object.freeze(MATH_CONSTANTS);

// Cambia solo el código encima de esta línea
try {
  MATH_CONSTANTS.PI = 99;
} catch(ex) {
  console.log(ex);
}
return MATH_CONSTANTS.PI;
}
const PI = freezeObj();

console.log(PI);
  **Información de tu navegador:**

El agente de usuario es: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36

Desafío: Prevenir la mutación del objeto

Enlaza al desafío:

The error is expected. That is just the result of trying to alter the value of the property MATH_CONSTANTS.PI.

MDN: freeze

Nothing can be added to or removed from the properties set of a frozen object. Any attempt to do so will fail, either silently or by throwing a TypeError exception (most commonly, but not exclusively, when in strict mode).

1 Like

Perfecto, ahora entendí.
Mi confusión radicaba en que pensé que el error me lo devolvía por utilizar Object.freeze(), pero gracias a tu respuesta, analicé y veo que el error lo devuelve por intentar asignar un valor a la constante MATH_CONSTANTS.

GRACIAS! :slightly_smiling_face: