Here's one way we're using ES6 proxies at Slack:<p><a href="https://github.com/paulcbetts/electron-remote" rel="nofollow">https://github.com/paulcbetts/electron-remote</a><p>Create a proxy for an ES6 module that actually creates a separate process in a task pool and executes the method there.
None of that code is any more clear, either conceptually or syntactically than a simply implemented and well designed library that does something purposeful and applied, like their "application" of handling validation.<p>And if you want "private" variables in javascript, just use a closure! Do people who write these blog posts even know basic javascript these days? If you really want all the features of java [said no one ever], than why don't you just.....use java??<p>It's just extra cruft we're going to have to support now in our javascript runtimes.<p></slams door>
This is a great article. Proxies really have gotten lost in the hype around ES6. Surely it's in part because JS developers have obvious pain points with async control flow and clumsy syntax, so things like generators and generous helpings of syntactic sugar are the most visible changes. However, I'm inclined to think that the level of abstraction and the complex API make it difficult for many developers to recognize their use cases or even play around with them.
I think another reason is that ES6 proxies are impossible to polyfill.<p>My favorite use case would be Facebook's immutable library[1], which uses awkward set(), setIn() and updateIn() methods for changing its data structures. But as an ES5+ library it wouldn't want to require the consuming code to do ES6 transpilation.<p>[1]: <a href="https://facebook.github.io/immutable-js/" rel="nofollow">https://facebook.github.io/immutable-js/</a>
A lot of these use cases are achievable using Object.defineProperty/Properties. In most cases I think defineProperty even provides a nicer API because it's per-property instead of per-trap. The 'has' trap can't be achieved completely with defineProperty (although you can make a property non-enumerable, which achieves the same goal in some cases).<p><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty" rel="nofollow">https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...</a>
I have an AI which spends a large portion of time cloning game state. I'd consider proxies to implement ergonomic copy-on-write if they were portable
> This may be due to slow or limited support in Safari (no versions support it)<p>It's worth noting that Safari Tech Preview actually does support it. It shows from the compatibility table that he links to. I also tested it just now, in Release 8.
another great use case is dependency injection as seen in <a href="https://larsjung.de/ghor/" rel="nofollow">https://larsjung.de/ghor/</a>