4 ms·
the core use case is var { foo } = pony I agree this is kind of silly in isolation, but more often than not, its used like this: var { foo, bar, baz } =
by aggronn 11y ago
the core use case is
var { foo } = pony
I agree this is kind of silly in isolation, but more often than not, its used like this:
var { foo, bar, baz } = pony
which is honestly not that hard to read, and is much better than
var foo = pony.foo;
var bar = pony.bar;
var baz = pony.baz;
which is very useful when you're referencing properties of pony a lot. Since you're going to see destructuring used like
var { foo, bar, baz } = pony
typically, I think its a very nice idea to always do your variable assignments like that even when its a little obtuse. I'd rather not see multiple styles for variable assignment.
var { foo: f } = pony
is just there as an escape hatch in case foo already exists in the namespace. It is not something you would typically do.
- sotojuan 11y agoI wish they'd let you return stuff this way, like: return {a, b, c} = foo;
- the_mitsuhiko 11y agoWhat would be the point of that? Just return `foo` and destructor in the callee.
- Retra 11y agoIt's not hard to read if you already know what it does. As it stands, it's a barrier to learning and a detriment to consistency in style.
- abecedarius 11y agoThat's a general argument against any added feature. The question is, is it useful enough? This particular feature seems valuable and cheap: I write code all the time that would be cleaner with it, and the meaning is natural enough that I independently invented essentially the same syntax years ago as a pair of Lisp macros (dealing in a-lists instead of hashtables).
- _greim_ 11y ago> As it stands, it's a barrier to learning and a detriment to consistency in style. Not true. It's actually super-easy to learn and meshes well with existing variable declaration styles.
- deleted 11y ago[deleted]
- Retra 11y agoI'm not sure why you spent two lines of text to say "I disagree."