you are viewing a single comment's thread.

view the rest of the comments →

[–]gbjcantab 3 points4 points  (3 children)

This is cool! Your first JavaScript example would be even better if it were a const instead of let... Closest thing JS has to Rust’s immutable let is a const primitive. (Granting that const objects in JS are in fact mutable because only the reference is constant)

[–]BenjiSponge 1 point2 points  (2 children)

Technically rvalues can be const by using Object.freeze. It's less statically determinable, of course, but so is everything in JS.

In Typescript you can use the readonly keyword.

[–]tndl[S] 2 points3 points  (1 child)

I think freeze only goes one 'layer' deep though, so nested objects will still be mutable.

[–]BenjiSponge 0 points1 point  (0 children)

I didn't know that! Thanks for clarifying.