Something like Object.freeze, but not inherited from prototypes.

# /#!/JoePea (6 years ago)

Currently, Object.freezeing a prototype affects all objects "inheriting" from the prototype when trying to modify props on an inheriting object when those same props exist on the prototype, which isn't the behavior that I want.

I'd like to prevent a prototype from being modifiable directly, but I still want objects that "inherit" from the prototype to work like normal (i.e. they can still shadow properties of the prototype and the object will be modified directly rather than the prototype).

For example, the following is my problem:

prototype.foo = "foo"

Object.freeze( prototype )

const o = Object.create( prototype )

o.foo = "bar" // I want this to work (but it doesn't)

prototype.foo = "baz" // and I want only this to fail because it is
modifying the prototype directly

In otherwords, I'd like for inheritance (reading values from the prototype, and shadowing the prototype) to work like normal, but I don't want someone to grab the prototype and modify it directly and affect all other instances of the "class".

Is there some way to do this currently? If not, I'd like to have such a feature so that only instances of my classes can be modified, but all prototypes in the inheritance are not.

I'm also noting that Object.seal and Object.preventExtensions behave the same way (they affect object that inherit from the prototype which is what I want to avoid).

# Isiah Meadows (6 years ago)

I find it odd and even a bit bizarre that [[Set]] even checks frozenness of the prototype for non-getter, non-setter descriptors, when it only adds own properties. Here's what I propose should change:

  1. If the descriptor used by [[Set]] is an accessor property, use that descriptor directly as is already done.
  2. If the descriptor used by [[Set]] is a data property, attempt to define an own data property on the target object.
  3. As already done, [[DefineOwnProperty]] throws when you attempt to modify a frozen object, even if the old and new properties differ only in identity.

Isiah Meadows me at isiahmeadows.com

Looking for web consulting? Or a new website? Send me an email and we can get started. www.isiahmeadows.com

# Isiah Meadows (6 years ago)

BTW, just filed tc39/ecma262#1154. It's bizarre enough it seemed like a spec bug to me.

Isiah Meadows me at isiahmeadows.com

Looking for web consulting? Or a new website? Send me an email and we can get started. www.isiahmeadows.com