开发者

Can the effect of Object.freeze be reversed in ES5?

开发者 https://www.devze.com 2023-04-05 20:27 出处:网络
Once I do this: var x = { }; Object.freeze( x )开发者_JS百科; Is there any way to modify x? Thanks. Nothing can be added to or removed from the properties set of a frozen

Once I do this:

var x = { };
Object.freeze( x )开发者_JS百科;

Is there any way to modify x? Thanks.


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

You can think about it like this:

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

So basically, freeze will set the objects writable and configurable flags to false after creation.


No, the idea of Object.freeze is that you cannot change it anymore. According to the documentation:

In essence the object is made effectively immutable.

and:

Nothing can be added to or removed from the properties set of a frozen object. Any attempt to do so will fail, ...

0

精彩评论

暂无评论...
验证码 换一张
取 消