0
0

More than 3 years have passed since last update.

How to Deep Freeze by JavaScript

Last updated at Posted at 2020-01-12

Code of Deep Freeze

 function deepFreeze(o) {
    Object.freeze(o);

    var objIsFunction = typeof o === 'function';

    Object.getOwnPropertyNames(o).forEach(function (prop) {
      if (o.hasOwnProperty(prop)
        && o[prop] !== null
        && (typeof o[prop] === "object" || typeof o[prop] === "function")
        && (objIsFunction ? prop !== 'caller' && prop !== 'callee' && prop !== 'arguments' : true)
        && !Object.isFrozen(o[prop])) {
        deepFreeze(o[prop]);
      }
    });

    return o;
  };
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0