3 ms·
I'm a bit puzzled what symbols are. If you want a unique thing that !== anything except itself, make an empty object. It seems to me: var foo = Symbol("foo") ou
by Tloewald 12y ago
I'm a bit puzzled what symbols are. If you want a unique thing that !== anything except itself, make an empty object. It seems to me: var foo = Symbol("foo") ought to let you dereference via bar.foo vs. bar[foo] (the latter shouldn't even work!) or it's not what it seems it wants to be.
- btipling 12y agoSymbol has other traits beyond just not being unique. Properties added via symbols are not iterated over in a for in and do not show up in calls to Object. getOwnPropertyNames()
- TazeTSchnitzel 12y agoThe point of it is you can implement private properties, as I understand it: function MyClass() { var mysymbol = new Symbol("foo"); this[mysymbol] = 3; } var bar = new MyClass(); bar.foo // this won't work
- Tloewald 12y agoWhat a convoluted mechanism!
- Tloewald 12y agoWhat a convoluted mechanism!
- Tloewald 12y agoWhat a convoluted mechanism! But where do you keep your reference to the symbol to keep it private? Anywhere you keep it (e.g. in a closure somewhere) that's private doesn't need symbols, so what problem does this solve? And surely in your private context, bar.foo should still work.