The base use case for JS symbols is not the same as Ruby symbols: regular strings will work just fine for that.
Symbols in ruby might have the same name but are fundamentally different. E.g. in ruby `:foo` and `:foo` are the same thing. In JavaScript, they are quite explicitly not the same thing. They share little in common (in terms of how they work and what they are designed to do) but the name.
N, E, S, W? I think you want an enum for that.
It was an example of replacing constants with symbols when the value of the constant really doesn't matter. Purposely not fancy stuff. I understand the advantages of enums (among the others, their values are constrained) but does JS have enums?
> does JS have enums?
No, but TypeScript and Dart do.
Enums don't really work without types.
But using Symbols for fake enums is probably a good idea.
class Enum {
constructor(...props) {
props.forEach(p => this[p] = Symbol(p));
}
}
const dir = Object.freeze(new Enum(...'NESW'));
console.log(dir.E === dir.E); // true
console.log(dir.E === dir.N); // false
Slightly awkward, but this might be about as good as it gets.
The most simple way to use them is to replace definitions like this one
The programmer is doing the work of the interprer/compiler here and make sure to pick unique values for all the constants that are going to be compared together. With symbols that becomes which is an improvement even if it is (in a traditional JavaScript way) so much more verbose than Ruby's I just wish they'll add some syntactical sugar to do without that "new Symbol()" thing and create symbols as needed like Ruby does.Unfortunately, from https://developer.mozilla.org/en/docs/Web/JavaScript/Referen...
We're going to manually serialize them when they leave the RAM.