zoukankan      html  css  js  c++  java
  • deepFreeze

    obj1 = {
      internal: {}
    };

    Object.freeze(obj1);
    obj1.internal.a = 'aValue';

    obj1.internal.a // 'aValue'

    // To make obj fully immutable, freeze each object in obj.
    //
     To do so, we use this function.
    function deepFreeze(obj) {

      // Retrieve the property names defined on obj
      var propNames = Object.getOwnPropertyNames(obj);

      // Freeze properties before freezing self
      propNames.forEach(function(name) {
        var prop = obj[name];

        // Freeze prop if it is an object
        if (typeof prop == 'object' && prop !== null)
          deepFreeze(prop);
      });

      // Freeze self (no-op if already frozen)
      return Object.freeze(obj);
    }

    obj2 = {
      internal: {}
    };

    deepFreeze(obj2);
    obj2.internal.a = 'anotherValue';
    obj2.internal.a; // undefined
  • 相关阅读:
    SpringSecurity开发
    SpringBoot 集成Spring Security
    Hexo
    gitbook使用
    Maze
    Party
    A. DZY Loves Chessboard
    1042B. Vitamins
    Petr and a Combination Lock
    433B.Kuriyama Mirai's Stones
  • 原文地址:https://www.cnblogs.com/shidengyun/p/5437564.html
Copyright © 2011-2022 走看看