Prevent Object Mutation I have a question about this

Tell us what’s happening:
This is the code I just practiced, so if I’m not mistaken, Object.freeze can freeze the function?

Your code so far


function freezeObj() {
  "use strict";
  const MATH_CONSTANTS = {
    PI: 3.14
  };
  // change code below this line
  Object.freeze(freezeObj)

  // change code above this line
  try {
    MATH_CONSTANTS.PI = 3.14;
  } catch( ex ) {
    console.log(ex);
  }
  return MATH_CONSTANTS.PI;
}
const PI = freezeObj();

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) coc_coc_browser/77.0.126 Chrome/71.0.3578.126 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/es6/prevent-object-mutation

Once the object is frozen, you can no longer add, update, or delete properties from it. Any attempt at changing the object will be rejected without an error.

Hi, you tried freeze one function inside itself, search the real object you need freeze. :male_detective::mag_right:

1 Like

Yeah, but what I want to ask is whether Object.freeze can freeze other things like Function or Array?

Both arrays and functions are objects, so yes you can use Object.freeze() with them. Although I’m not really sure I see a point of using it on a function.

function test() {
  console.log('test')
}

test.prop = 'some prop'
test.prop
// returns "some prop"


function test() {
  console.log('test')
}

Object.freeze(test);
test.prop = 'some prop'

test.prop
// returns undefined
2 Likes