4 ms·
Great writeup. I welcome most of the additions, but I somehow cannot get behind things like: var {foo} = pony is equivalent to var foo = pony.foo var {foo: baz
by gramakri 11y ago
Great writeup. I welcome most of the additions, but I somehow cannot get behind things like:
var {foo} = pony is equivalent to var foo = pony.foo
var {foo: baz} = pony is equivalent to var baz = pony.foo
I am not sure why, but most languages get into a state where they seem to encourage non-readable code. What was wrong with 'var baz = pony.foo' to start with?
- aggronn 11y agothe 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."
- bryanlarsen 11y agoI find it most useful for import statements: import {Button, Text} from 'react-native'; and in function declarations: function f({foo, bar})
- the_mitsuhiko 11y agoHowever: import statements do not actually destruct the default export, it does something entirely different. It looks up exported bindings.
- nostrademons 11y agoIt's pretty handy when unpacking a JSON blob so you can manipulate individual fields: let { b64_img, height, width, filename } = JSON.parse(text)
- z3t4 11y agoIt's the new with.
- Sir_Cmpwn 11y agovar { email, full_name } = get_user(); send_email(email, `Hi ${full_name}!`);