Files
Randell Dawson 1494a50123 fix(guide): restructure curriculum guide articles (#36501)
* fix: restructure certifications guide articles
* fix: added 3 dashes line before prob expl
* fix: added 3 dashes line before hints
* fix: added 3 dashes line before solutions
2019-07-24 13:29:27 +05:30

803 B

title
title
Prevent Object Mutation

Prevent Object Mutation


Hints

Hint 1

  • Use Object.freeze() to prevent mathematical constants from changing.

Solutions

Solution 1 (Click to Show/Hide)
function freezeObj() {
  "use strict";
  const 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();

Code Explanation

  • By using Object.freeze() on MATH_CONSTANTS we can avoid manipulating it.