r/JSdev • u/getify • Jun 25 '21
Symbols... good or bad addition to JS?
Do you think symbols were a smart idea to add to JS?
There's built-in symbols for various metaprogramming hooks, which seem reasonable. But what about user-created symbols? Are they just all show and no substance?
I've taught and defended them dozens of times, but I confess that in the back of my mind I'm kinda meh.
Agree with me? Or am I missing the benefits?
3
u/adiabatic Jun 25 '21
I had a look at what they do and it seems like they solve a problem that other people have that I don't.
However, they seem to solve problems that library authors have, so I'm weakly in favor.
2
1
u/dmail06 Jun 25 '21
I think I have used them once to ensure an object has a custom symbol before operating on it. Apart from that, I think Symbol.iterator is the most impactful and main use case for symbols. It's a powerful pattern that might be useful to JS one day for something else than iterator so I'm glad they exist.
1
u/AndrewGreenh Jun 25 '21
Big fan, just for the purpose of the iterator protocol.
It's a quite clever way to add "interfaces" or contracts to a dynamically typed language.