一旦我这样做:
var x = { };
Object.freeze( x );
有什么办法可以修改x吗? 谢谢。
最佳答案
Nothing can be added to or removed from the properties set of a frozen object. Any attempt to do so will fail, either silently or by throwing a TypeError exception (most commonly, but not exclusively, when in strict mode).
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/freeze
你可以这样想:
if( typeof ChuckNorris === 'undefined' ) {
ChuckNorris = Object.create( [Infinity], {
canCountTo: {
value: Infinity * 2,
writable: true,
configurable: true
}
});
Object.freeze( ChuckNorris ); // nothing can harm Chuck anymore !
}
console.log( ChuckNorris.canCountTo ); // Infinity
delete ChuckNorris.canCountTo;
console.log( ChuckNorris.canCountTo ); // Infinity
所以基本上,freeze
会在创建后将对象的 writable
和 configurable
标志设置为 false。
关于javascript - ES5 中 Object.freeze 的效果可以逆转吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7442748/