A partial archive of discourse.wicg.io as of Saturday February 24, 2024.

[Proposal] Make const object immutable

goatandsheep
2018-10-31

If I declare an object using const, I shouldn’t be able to use push(), pull(), shift(), nor slice() on it for it to be truly immutable.

jhpratt
2018-10-31
  1. This would be a breaking change, which is a non-starter.
  2. Use Object.freeze and Object.seal to do what you want.
goatandsheep
2018-11-01

that’s amazing. Thank you for that!

simevidas
2018-11-03

const means the binding can’t change, but it doesn’t say anything about the object that is assigned to that binding. That’s just how this feature was defined.

That being said, it may be useful to have a way to declare a variable whose value is automatically immutable. Something like:

ice arr = [1, 2, 3];

Or not, I don’t really know :sweat_smile: