3 ms·
Using const and `splice` breaks the rules: const info = [1,2,3,4] const newInfo = info.splice(2); 'info' has changed
by neximo64 10y ago
Using const and `splice` breaks the rules:
const info = [1,2,3,4]
const newInfo = info.splice(2);
'info' has changed
- josephg 10y agoThe const keyword makes the object reference constant. It doesn't make the object's value constant. You can change the contents of 'info' (info.push(5) is fine). You just can't change which object the variable points to. (info = [] will throw). If you know C/C++, the code 'const info = []' makes 'info' a constant pointer to a list not a pointer to a constant list. If you want to stop a variable from being changed, use Object.freeze() - https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze https://developer.mozilla.org/en/docs/Web/JavaScript/Referen...
- samcodes 10y agoThis was what I came to comment on; when introducing ES6 features, this always trips people up. Thanks for "constant pointer to a list, not pointer to a constant list", I will use that next time I explain this. This also means that objects defined with const can be mutated: const x = { y: 2 }; x.y = 3; Doesn't error. However with types that are immutable, like numbers and strings, const really is a constant: const HELLO = "hi" will never change.
- deleted 10y ago[deleted]
- robrichard 10y agoconst only guards against changing the reference that it was assigned to. This won't break because info is still assigned to the same array. It doesn't matter that the array was mutated.
- Tankenstein 10y agoCommon misconception, const only disallows reassignment. For instance, const number = 1337; number = 10; // fails But objects/arrays are not immutable in js, so you can do this: const person = { name: 'Dude' }; person.name = 'Dudette'; Which is perfectly valid. If you want full immutability, i recommend you check out https://facebook.github.io/immutable-js/ https://facebook.github.io/immutable-js/, been running it in production, a real pleasure to work with.
- jasoncchild 10y agoFWIW Immutable.js is not deeply immutable. Seamless-immutable however is.
- masklinn 10y ago> Seamless-immutable however is. Only in development mode, by freezing the objects.
- Roboprog 10y agoAny popular "deep freeze" utilities around?
- pitaj 10y agofunction deepFreeze(obj) { const out = Object.freeze(obj); Object.keys(out).forEach(key => { if (typeof out[key] === 'object') { out[key] = Object.freeze(out[key]); } }); return out; }
- Roboprog 10y agoI think you meant "out[ key ] = deepFreeze( out[ key ] )" in the innermost step :-) (recursive)
- Cthulhu_ 10y agoThe only language I've found / used so far that has these (expected) mechanics is Swift, which, when using a `let` for e.g. a list, will make the list itself immutable (in addition to the reference). It's really something that is confusing in every language that has some form of 'final', be it Java (which added immutable list implementations or wrappers to their existing collections), C++, JS, or what-have-you.
- masklinn 10y ago> The only language I've found / used so far that has these (expected) mechanics is Swift Rust has similar semantics: https://is.gd/hsRjPh https://is.gd/hsRjPh versus https://is.gd/ROgzXl https://is.gd/ROgzXl IIRC you can also declare const objects in C++, but constness is more complex and overridden there.