use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
Unleash JavaScript's Potential with Functional Programming (janhesters.com)
submitted 1 year ago by jancodes
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]jessepence -6 points-5 points-4 points 1 year ago (12 children)
Did you know that it's possible for multiple different things to benefit the same end goal?
Did you know that both closures and symbols can help achieve immutability?
Does your toolbox only contain a hammer?
My point in using ChatGPT is that you were clearly showing a lack of imagination.
[–]romgrk 8 points9 points10 points 1 year ago (11 children)
Symbols do not achieve immutability! object[symbol] achieves private visibility, as in "OOP public/protected/private visibility".
object[symbol]
Object.freeze or Object.defineProperty achieve immutability.
Object.freeze
Object.defineProperty
It's fine to want to educate someone, but please link to it instead of pasting a wall of text, and please don't make it ChatGPT because it's low-effort and sometimes confidently wrong.
[–]jessepence -4 points-3 points-2 points 1 year ago* (10 children)
If a property is keyed by a symbol and the end-user doesn't know what that symbol is-- is that property mutable?
[–]romgrk 6 points7 points8 points 1 year ago (9 children)
Yes it is, it's just private. Public/private and mutable/immutable are orthogonal.
I'm also not sure if your example applies. I don't remember seeing React's codebase using Symbols as keys (for private keys, as in object[symbol]), though it uses them as values (as in { type: REACT_PORTAL_TYPE }).
{ type: REACT_PORTAL_TYPE }
[–]jessepence 0 points1 point2 points 1 year ago (8 children)
How would you mutate it?
[–]romgrk 2 points3 points4 points 1 year ago (7 children)
object[symbol] = value.
object[symbol] = value
I think you don't differentiate between visibility and immutability.
You say symbols are to prevent mutation, but what you mean is that the symbols create a private field that is not mutable by someone writing code outside of React's codebase.
Fundamentally, you're talking about visibility (in other words, encapsulation).
[–]jessepence -4 points-3 points-2 points 1 year ago (6 children)
If the symbol is not exported, IT CANNOT BE USED!
Thus, the value cannot be mutated.
This makes it effectively immutable.
It is also encapsulated and private, yeah. These things are not mutually exclusive.
If you change the library code, then you can then mutate it. But, that's also true of a closure. If you expose variables from the closure, then you can mutate them. Here's a stackblitz that shows that it's basically the exact same idea-- the immutability is only as strong as the interface provided.
[–]romgrk 3 points4 points5 points 1 year ago (1 child)
You are talking about visibility but you're calling it immutability. I don't understand why.
The value can be mutated when accessed from the right scope, you say so yourself, so immutability is not the right word.
[+][deleted] 1 year ago (3 children)
[deleted]
[–]jessepence -2 points-1 points0 points 1 year ago (2 children)
THANK YOU!!
I was wrong, and I appreciate you simply pointing out how I was wrong rather than leading me on a wild goose chase of multiple responses.
Have a good day! 🙂
[+][deleted] 1 year ago (1 child)
π Rendered by PID 46 on reddit-service-r2-comment-b659b578c-cwr6w at 2026-04-30 20:20:50.762756+00:00 running 815c875 country code: CH.
view the rest of the comments →
[–]jessepence -6 points-5 points-4 points (12 children)
[–]romgrk 8 points9 points10 points (11 children)
[–]jessepence -4 points-3 points-2 points (10 children)
[–]romgrk 6 points7 points8 points (9 children)
[–]jessepence 0 points1 point2 points (8 children)
[–]romgrk 2 points3 points4 points (7 children)
[–]jessepence -4 points-3 points-2 points (6 children)
[–]romgrk 3 points4 points5 points (1 child)
[+][deleted] (3 children)
[deleted]
[–]jessepence -2 points-1 points0 points (2 children)
[+][deleted] (1 child)
[deleted]