继续第二部分

Object.freeze(obj)

看字面意思就是“把一个对象冻结”。

下面我们来看个简单的例子以作说明:

     // a person instance
var person = {
name: 'Andrew',
job: 'sales manager'
}; // before freeze
// existing properties maybe be changed or removed, new properties may be added
person.name = 'Bruce';
person.age = 30; console.log(person.name); // Bruce
console.log(person.age); // // freeze the person
Object.freeze(person); // isFrozen
console.log( Object.isFrozen(person) ); // true // after freeze
// fail silently, or strict mode throw a TypeError
person.name = 'James';
person.age = '35';
person.gender = 'male'; console.log(person.name); // Bruce
console.log(person.age); //
console.log(person.gender); // undefined

上面例子的特点是,person对象的属性像name,job,age的值都是“字面值”,并非对象,如果存在一个属性,其值是一个对象呢?

我们再来一个例子:

     // a person again
var person = {
name: {
firstname: 'Bruce',
lastname: 'Lee'
},
age: 35,
job: 'sales manager',
children: ['lucy', 'linda']
}; console.log('name:' + person.name.firstname + ' ' + person.name.lastname); // name:Bruce Lee
console.log('children:' + person.children); // lucy, linda // freeze the person
Object.freeze(person); // has been frozen!
console.log(Object.isFrozen(person)); // change name
person.name.firstname = 'Andrew';
person.name.lastname = 'Carnegie'; // add one more child
person.children.push('Tom'); console.log('name:' + person.name.firstname + ' ' + person.name.lastname); // name:Andrew Carnegie
console.log('children:' + person.children); // lucy, linda, Tom

通过上面的2个例子,我们很容易得出结论。那么我们如何做到“深层”freeze呢。

就要用到一个递归函数,来让里面的每个对象都被freeze:

     function deepFreeze(o) {
var prop, propkey; Object.freeze(o); for(propkey in o) {
prop = o[propkey]; if( !o.hasOwnProperty(propkey) || !(typeof prop === 'object') || Object.isFrozen(prop) ) {
continue;
}
// Recursively call deepFreeze
deepFreeze(prop);
}
}; // if we use deepFreeze to replace with Object.freeze in the previous example,
// then nothing will happen, even person's properties(aka. name, age, children ) are changed

利用的deepFreeze方法,我们就可以把person对象的属性全部“冻结”。


搞清楚Object.freeze的用途后,我们继续来了解下Object.seal

Object.seal(obj)

seal从字面意思上理解有“密封”的意思,我依旧通过例子来说明:

     // a person again
var person = {
name: 'Andrew',
age: 25,
children: ['Tom'],
edu: {
major: 'computer science',
university: 'Yale'
}
}; // before seal
// new properties may be added, exisiting properties may be changed or removed
person.name = 'Bruce';
person.gender = 'male';
delete person.age; // looking up the structure
console.dir(person); // seal the person
Object.seal(person); // has been sealed
console.log( Object.isSealed(person) ); // true // after seal
// existing (writable) properties can be changed
person.name = 'Jackson';
console.log(person.name); // Jackson // or change value through Object.defineProperty
Object.defineProperty(person, 'name', {value: 'Ann'});
console.log(person.name); // Anna // add one more child
person.children.push('lucy');
console.log(person.children); // tom, lucy // internal object is still extensible, unless you seal it too (eg. Object.seal(person.edu) )
person.edu.degree = 'master';
console.log(person.edu.degree); // master // silently fail or in strict mode throw TypeError
// when add or delete property
person.job = 'Front End Developer';
delete person.name;
// TypeError when add property through Object.defineProperty
Object.defineProperty(person, 'job', {value: 'teacher'}); // TypeError

通过上面的例子,我们可以看到,seal一个对象后,使得它不可扩展,但是其属性值(该属性writable为true)是 可以改变的。还有一点就是若其属性是一个对象,姑且叫内部对象吧,那么这个内部对象还是可以进行扩展的,除非写一个递归的deepSeal方法, 这个可以参考上面的deepFreeze

第二部分就到这里,感兴趣的同学可以前往MDN查看,Object.freeze,Object.seal。

ECMAScript5之Object学习笔记(二)的更多相关文章

  1. ECMAScript5之Object学习笔记(一)

    随着IE的逐步追赶,目前到IE11已经能够很好的支持ECMAScript5标准了,其他的现代浏览器像firefox,chrome,opera就更不用说了. 再加上nodejs使得javascript在 ...

  2. ECMAScript5之Object学习笔记(三)

    第三部分继续... Object.getOwnPropertyDescriptor(obj, prop) 获取一个对象的属性描述符 根据"Own"这个词我们可以猜到,prop只能是 ...

  3. JavaScript Object学习笔记二

    Object.create(proto, [propertiesObject])//创建对象,使用参数一来作为新创建对象的__proto__属性,返回值为在指定原型对象上添加自身属性后的对象 //参数 ...

  4. WPF的Binding学习笔记(二)

    原文: http://www.cnblogs.com/pasoraku/archive/2012/10/25/2738428.htmlWPF的Binding学习笔记(二) 上次学了点点Binding的 ...

  5. AJax 学习笔记二(onreadystatechange的作用)

    AJax 学习笔记二(onreadystatechange的作用) 当发送一个请求后,客户端无法确定什么时候会完成这个请求,所以需要用事件机制来捕获请求的状态XMLHttpRequest对象提供了on ...

  6. JMX学习笔记(二)-Notification

    Notification通知,也可理解为消息,有通知,必然有发送通知的广播,JMX这里采用了一种订阅的方式,类似于观察者模式,注册一个观察者到广播里,当有通知时,广播通过调用观察者,逐一通知. 这里写 ...

  7. Typescript 学习笔记二:数据类型

    中文网:https://www.tslang.cn/ 官网:http://www.typescriptlang.org/ 目录: Typescript 学习笔记一:介绍.安装.编译 Typescrip ...

  8. 学习笔记(二)--->《Java 8编程官方参考教程(第9版).pdf》:第七章到九章学习笔记

    注:本文声明事项. 本博文整理者:刘军 本博文出自于: <Java8 编程官方参考教程>一书 声明:1:转载请标注出处.本文不得作为商业活动.若有违本之,则本人不负法律责任.违法者自负一切 ...

  9. ES6学习笔记<二>arrow functions 箭头函数、template string、destructuring

    接着上一篇的说. arrow functions 箭头函数 => 更便捷的函数声明 document.getElementById("click_1").onclick = ...

随机推荐

  1. 【BZOJ 2724】 2724: [Violet 6]蒲公英 (区间众数不带修改版本)

    2724: [Violet 6]蒲公英 Time Limit: 40 Sec  Memory Limit: 512 MBSubmit: 1908  Solved: 678 Description In ...

  2. BZOJ.2916.[POI1997]Monochromatic Triangles(三元环)

    题目链接 \(Description\) n个点的完全图,其中有m条边用红边相连,其余边为蓝色.求其中三边同色的三角形个数. \(Solution\) 直接求同色 除了n^3 不会.. 三角形总数是C ...

  3. mac os 切换网络优先级

    升级到新系统OS X Yesemite 系统后有WIFI时会默认使用WIFI而不是有线. 但是公司的WIFI基本没法用,每次到公司之后就得把WIFI关掉,回家又打开,烦死了. 今天研究了下原来网络优先 ...

  4. Windows下C语言调用dll动态链接库

    dll是windows下的动态链接库文件,下面记录一下在windows下如何调用C语言开发的dll动态链接库. 1.dll动态链接库的源代码 hello_dll.c #include "st ...

  5. 模板 快速询问GCD

    快速询问两个数的GCD 我觉得只有智障会卡这个玩意儿-- const int maxn = 1e6; const int Sqrt_N = 1e3; int pre[maxn + 1] , decom ...

  6. tyvj 1044 数字三角形 记忆化搜索

    数字三角形 Time Limit: 1 Sec  Memory Limit: 162 MB 题目连接 http://www.tyvj.cn/p/1044 Description 示出了一个数字三角形. ...

  7. ASP.NET 构建高性能网站 第3篇

    HTTP请求的优化 在一个网页的请求过程中,其实整个页面的html结构(就是页面的那些html骨架)请求的时间是很短的,一般是占整个页面的请求时间的10%-20%.在页面加载的其余的时间实际上就是在加 ...

  8. NGINX如何反向代理Tomcat并且实现Session保持

    简介 LNMT=Linux+Nginx+MySQL+Tomcat: Tomcat 服务器是一个免费的开放源代码的Web 应用服务器,属于轻量级应用服务器: 在中小型系统和并发访问用户不是很多的场合下被 ...

  9. 三个实例演示 Java Thread Dump 日志分析(转)

    原文链接:http://www.cnblogs.com/zhengyun_ustc/archive/2013/01/06/dumpanalysis.html 转来当笔记^_^ jstack Dump ...

  10. SmartProg2 Universal, ISP capable programmer

    http://www.elnec.com/products/universal-programmers/smartprog2/ 40 powerful TTL pindrivers provide H ...