Prevent Object.seal

Is there a way of preventing other's code from sealing my objects and/or prototypes?

Not if you hand out the object. You could provide a Proxy to the object, perhaps?

1 Like

You can achieve that by monkeypatching Object.seal, but I don't think it's a good way.

I agree and if Object.seal were ever made non configurable in the future that would break things.

I find it interesting that I can seal my objects but I can't prevent others from sealing them in a supported way (from what I can tell).

I wonder if we need an Object.open or similar to block calls like preventExtensions, seal, and freeze on objects so that they cannot be tampered with.

What is your use case that relies on being able to mutate an object you’ve exposed?

1 Like

Have you considered returning a proxy whose preventExtensions returns false?

return new Proxy(object, {
    preventExtensions: () => false,
})

If you return this instead of the object directly, Object.preventExtensions(proxy), Object.seal(proxy), and Object.freeze(proxy) will all fail, the first because the boolean result is proxied to the call, thus causing the method itself to throw a TypeError and the second because they attempt to block further extension before attempting to modify descriptors and skip that step if that itself fails and of course those two methods also throw if those fail early.

I was not aware of that as a possibility. Thank you!