Re:Advanced objects in JavaScript
by AgentME on Sunday August 24, 2014 @08:27PM
(#8220095)
Everything there besides Proxies and Symbols is already in common browsers, and I can't say I'm familiar with Proxies and Symbols in other languages (not to say some other languages necessarily don't have them, but I don't think most users consider them basic or encounter them often).
[ Parent
| on HN ]
Re:Advanced objects in JavaScript
by TazeTSchnitzel on Monday August 25, 2014 @05:47AM
(#8221139)
Proxies are like __get/__call in PHP, method_missing in Ruby and the similar magic methods Python has.
Symbols are sort of JavaScript's answer to private members. Closest to __ for members in Python.
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.
Symbol 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()
Re:Advanced objects in JavaScript
by TazeTSchnitzel on Monday August 25, 2014 @11:40AM
(#8222237)
The 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 workWhat 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.