2 ms·
In ES3 you'd need destructors, I think. But yes, agreed on all points :) And yet-- is the Symbol approach not metaprogramming? It's clearly a much saner way to
by Cushman 11y ago
In ES3 you'd need destructors, I think. But yes, agreed on all points :)
And yet-- is the Symbol approach not metaprogramming? It's clearly a much saner way to shoehorn this feature in, but it's still a shoehorn. And the underlying magic in both cases is in fact simple closure scope, which is really what I wanted to demonstrate.
Edit: And I guess my broader point, to make this more constructive, is that in JS these kinds of idioms live on a continuum of metaprogramming, and for the sanity of your co-contributors you usually want to be doing as little of that as necessary. Like others, I'm wary of presenting something like your example as a "design pattern" without that context.
Edit 2: To illustrate what I mean, I swear to you that if this pattern is adopted widely you will find code like this in the wild:
... = > {
let accountNumberKey = Symbol('accountNumber');
return class Account {
constructor (accountNumber) {
this[accountNumberKey] = accountNumber;
this.accountNumberKey = accountNumberKey; // ??? but transaction log works now
...