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

deepFreeze的更多相关文章

  1. 浅解析js中的对象

    浅解析js中的对象 原文网址:http://www.cnblogs.com/foodoir/p/5971686.html,转载请注明出处. 前面的话: 说到对象,我首先想到的是每到过年过节见长辈的时候 ...

  2. My ECMAScript 7 wishlist

    With ECMAScript 6 now feature complete, any further changes to the core of JavaScript will happen in ...

  3. [Redux] Reducer Composition with Arrays

    In the previous lesson we created a reducer that can handle two actions, adding a new to-do, and tog ...

  4. [Redux] Writing a Todo List Reducer (Toggling a Todo)

    Learn how to implement toggling a todo in a todo list application reducer. let todo = (state = [], a ...

  5. [Redux] Writing a Todo List Reducer (Adding a Todo)

    Learn how to implement adding a todo in a todo list application reducer. let todo = (state = [], act ...

  6. [Redux] Avoiding Object Mutations with Object.assign() and ...spread

    Learn how to use Object.assign() and the spread operator proposed for ES7 to avoid mutating objects. ...

  7. [Redux] Avoiding Array Mutations with concat(), slice(), and ...spread

    For Redux, you cannot use mutable methods like push, splice. Need to use immutable methods such as c ...

  8. javascript EcmaScript5 新增对象之Object.freeze

    我们都知道在js里对象是很容易改变的 var obj1 ={ a:'111' } obj1.a = '222'; console.log( obj.a ) //output 222 对象的属性发生了变 ...

  9. (74)Wangdao.com第十三天_Object 对象_属性描述对象

    Object 对象 JavaScript 原生提供 Object 对象 JavaScript 的所有其他对象都继承自  Object 对象,即那些对象都是Object的实例 Object 对象的原生方 ...

随机推荐

  1. 使用JS提交表单

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. vue.js(14)--自定义全局指令

    <input type="text" class="form-control" v-model="keywords" v-focus& ...

  3. ideamaven版的MBG逆向工程

    一.简介 简称MBG,是一个专门为MyBatis框架使用者定制的代码生成器,可以快速的根据表生成对应的映射文件,接口,以及bean类. 支持基本的增删改查,以及QBC风格的条件查询. 但是表连接.存储 ...

  4. 2018-11-26-win10-UWP-Controls-by-function

    title author date CreateTime categories win10 UWP Controls by function lindexi 2018-11-26 20:0:6 +08 ...

  5. Codeforces Round #425 (Div. 2) - B

    题目链接:http://codeforces.com/contest/832/problem/B 题意:给定一个好字母集合(只有小写字母,除了这些外其余都是坏字母集合),给定一个匹配模式串, 模式串只 ...

  6. linux --memcached的安装与配置

    转载:http://blog.sina.com.cn/s/blog_4829b9400101piil.html 1.准备安装包:libevent-2.0.21-stable.tar.gz 和memca ...

  7. centos 6.5 安装 jdk 8

    首先,检查是否已安装jdk,如果有,要先删除 rpm -qa|grep java rpm -e --nodeps filename 然后,从oracle官方网站下载jdk安装包:jdk-8u121-l ...

  8. Test 6.24 T2 集合

    问题描述 有一个可重集合,一开始只有一个元素 0. 你可以进行若干轮操作,每轮你需要对集合中每个元素 x 执行以下三种操作之一: 将 x 变为 x+1; 选择两个非负整数 y,z 满足 y+z=x , ...

  9. SpringBoot自定义Jackson配置

    为了在SpringBoot工程中集中解决long类型转成json时JS丢失精度问题和统一设置常见日期类型序列化格式,我们可以自定义Jackson配置类,具体如下: import com.fasterx ...

  10. sublime text3 Package Control 插件安装及推荐(MAC)

    参考: https://www.zhihu.com/question/36233553 https://www.cnblogs.com/zuoan-oopp/p/6692797.html 插件推荐 因 ...