Comments (3)
谢谢指出,原来的例子确实不当,已经改过来了。
from jstutorial.
现在的版本仍然有个描述上的小问题
但是,出于历史原因,这时依然可以将writable从true变成false,即seal方法对writable属性无效。
seal 之后是无法把 writable 从 false 变成 true 的,而 seal 之前可以。
仅仅是 seal 前后都可以将 writable 从 true 变为 false 。
seal方法对writable属性无效 的措辞个人认为有点欠妥。这句话的理解应该是 seal 前后 对 writable 属性的修改 这个操作的结果不产生任何影响,但实际上如上文所述无法将 writable 从 false 变为 true 了。
测试环境:Firefox 33.0.2
测试代码:
var o = {p : 0}; Object.getOwnPropertyDescriptor(o, 'p'); /* 未封闭 */ Object.defineProperty(o, 'p', {writable: false}); Object.getOwnPropertyDescriptor(o, 'p'); Object.defineProperty(o, 'p', {writable: true}); Object.getOwnPropertyDescriptor(o, 'p'); /* 封闭后 */ Object.seal(o); Object.defineProperty(o, 'p', {writable: false}); Object.getOwnPropertyDescriptor(o, 'p'); Object.defineProperty(o, 'p', {writable: true}); Object.getOwnPropertyDescriptor(o, 'p');
from jstutorial.
谢谢指出,改成下面这样了。
可写性(writable)有点特别。如果writable为false,使用Object.seal方法以后,将无法将其变成true;但是,如果writable为true,依然可以将其变成false。
var o1 = Object.defineProperty({}, 'p', {writable: false});
Object.seal(o1);
Object.defineProperty(o1,'p',{writable:true})
// Uncaught TypeError: Cannot redefine property: p
var o2 = Object.defineProperty({}, 'p', {writable: true});
Object.seal(o2);
Object.defineProperty(o2,'p',{writable:false})
Object.getOwnPropertyDescriptor(o2, 'p')
/* { value: '',
writable: false,
enumerable: true,
configurable: false } */
上面代码中,同样是使用了Object.seal方法,如果writable原为false,改变这个设置将报错;如果原为true,则不会有问题。
from jstutorial.
Related Issues (20)
- jstutorial/bom/window.md 中有个错误 HOT 1
- Array.prototype.splice.call() 并不能把对象转换为含有元素的数组 HOT 1
- storage事件问题 HOT 1
- 同源政策一节多了一个字符 HOT 1
- history对象 HOT 2
- 关于 phantmjs 抓取网页报变量未找到 HOT 1
- 正则表达式部分的一个小错误 HOT 2
- 想咨询一个问题 HOT 1
- contentEditable is readable HOT 4
- 老师,你好,网页版的在手机上排版有点问题 HOT 1
- Element对象这篇文章中offsetHeight描述有误 HOT 1
- Element.getClientRects() 关于行内描述问题 HOT 1
- 关于事件模型中Event.detail中代码示例问题 HOT 1
- 关于KeyboardEvent中代码示例问题 HOT 1
- JavaScript标准参考教程中dom模型概述有一段话描述是不是有问题. HOT 3
- commonJS规范 第 5节 模块的加载机制 描述有误 HOT 1
- prototype 对象章节 HOT 1
- 错字
- webRTC的一个小问题 HOT 1
- 是 req.params.id 还是 req.params.who HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jstutorial.