Tell us what’s happening:
I used Object.freeze() on MATH_CONSTANTS to freeze the variable so it is no longer mutable. However, I am getting the error:
[TypeError: Cannot assign to read only property ‘PI’ of object ‘#’]
This error is happening even when I copy the exact solution in GET A HINT which is exactly what I wrote in the first place.
What is happening here?
Thanks!
You should not change original `MATH_CONSTANTS` .
`PI` should equal `3.14` .
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
function freezeObj() {
"use strict";
let MATH_CONSTANTS = {
PI: 3.14
};
Object.freeze(MATH_CONSTANTS);
try {
MATH_CONSTANTS.PI = 99;
} catch (ex) {
console.log(ex);
}
return MATH_CONSTANTS.PI;
}
const PI = freezeObj();
[TypeError: Cannot assign to read only property 'PI' of object '#<Object>']
You should not change original `MATH_CONSTANTS` .
`PI` should equal `3.14` .
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
function freezeObj() {
"use strict";
let MATH_CONSTANTS = {
PI: 3.14
};
Object.freeze(MATH_CONSTANTS);
try {
MATH_CONSTANTS.PI = 99;
} catch (ex) {
console.log(ex);
}
return MATH_CONSTANTS.PI;
}
const PI = freezeObj();
[TypeError: Cannot assign to read only property 'PI' of object '#<Object>']
Object.freeze(MATH_CONSTANTS);
Your code so far
function freezeObj() {
"use strict";
let MATH_CONSTANTS = {
PI: 3.14
};
Object.freeze(MATH_CONSTANTS);
try {
MATH_CONSTANTS.PI = 99;
} catch (ex) {
console.log(ex);
}
return MATH_CONSTANTS.PI;
}
const PI = freezeObj();
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 11_1_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36
.
Challenge: Prevent Object Mutation
Link to the challenge: