johnny-john
johnny-john

Reputation: 854

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

Once I do this:

var x = { };
Object.freeze( x );

Is there any way to modify x? Thanks.

Upvotes: 2

Views: 382

Answers (2)

jAndy
jAndy

Reputation: 236022

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.

Upvotes: 2

pimvdb
pimvdb

Reputation: 154848

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, ...

Upvotes: 1

Related Questions