I'm trying to freeze below object without using Object.freeze()
is it possible to do so ?
const obj = {
a:'test',
b:'Something'
}
I'm trying to freeze below object without using Object.freeze()
is it possible to do so ?
const obj = {
a:'test',
b:'Something'
}
Not literally, no. Object.freeze
sets the integrity level of the object to frozen
, which you can't do in another way.
You can do most of the things that make an object "frozen" separately, though, by using Object.preventExtensions
to prevent properties being added and the combination of Object.getOwnPropertyDescriptor
and Object.defineProperty
to prevent properties being modified or removed. I don't think you can prevent its prototype being changed via Object.setPrototypeOf
, though, which is another aspect of frozen objects. (You could interfere with setting the __proto__
property inherited from Object.prototype
[if the object inherits from Object.prototype
], but that wouldn't have any effect on Object.setPrototypeOf
.)
But you can't actually set the integrity level of the object like Object.freeze
does.