5 ms·
> const o = {}; o.prop = "something"; // oh, actually, o.prop = 5; Care to share some examples / use cases where using these language characteristics can resul
by yarinr 6y ago
> const o = {}; o.prop = "something"; // oh, actually, o.prop = 5;
Care to share some examples / use cases where using these language characteristics can result in better, more expressive, more readable code?
(Not saying there aren't. I've been wondering about it myself and am genuinely curious)
Thanks!
- stephenr 6y agoI mean... let’s keep in mind we’re talking about a language and community that uses the keyword “const” to define mutable objects. Logic went out the window a long fucking time ago.
- dragonwriter 6y ago> we’re talking about a language and community that uses the keyword “const” to define mutable objects. Nope they use const to define immutable references, the same way that val/var define mutable references. If you don't understand the difference between mutability of a reference and mutability of the value it refers to, JavaScript is far from the only language you’ll have a problem with.
- stephenr 6y agoI understand the difference. The problem is that you can completely change the shape of the object that’s “const”, so it’s hardly fucking constant is it.
- dragonwriter 6y ago> I understand the difference No, you clearly don't. > The problem is that you can completely change the shape of the object that’s “const” “const” is not a feature of objects, but of the reference to a value (which may or may not be an object). You can't change the reference at all (unlike a var/let references), hence it is constant. Object immutability is orthogonal to reference immutability and is attained by Object.freeze().
- alcover 6y agodragonwriter is right. In C for ex. const int* p means the value pointed at can't change, but int* const p means you can't reaffect p