4 ms·
Am I missing something new with object literals or is this an error: window.state.notes[id] = { id, content: '' };
by emehrkay 9y ago
Am I missing something new with object literals or is this an error:
window.state.notes[id] = {
id,
content: ''
};
- lioeters 9y agoIf you mean this line: id, ..it's an "object literal property value shorthand", equivalent to: id: id,
- acemarke 9y agoLooks like a standard ES6+ object literal to me. ES6 allows you to shorthand `{someVariable : someVariable}` as just `{someVariable}` (ie, create a key whose name is the variable name and whose value is the variable value). There's some good overviews of ES6's new syntax and features at https://ponyfoo.com/articles/es6 https://ponyfoo.com/articles/es6 and http://es6-features.org/ http://es6-features.org/ .
- emehrkay 9y agoThanks that is exactly what I was asking. I didn't know about that change.
- deleted 9y ago[deleted]
- bpicolo 9y agoRust just added this syntax as well (personally, not a fan). Would rather not have the temporary var in the first place
- RussianCow 9y agoThe syntax isn't meant for you to have to create a temporary variable in order to use it; that would be pretty pointless. It's meant for cases when you already have a variable with the same name as the key you want to set.
- bpicolo 9y agoRight. But in almost all cases I'd prefer to do away with it entirely, and not encourage extra variables for the sake of usage. It makes code more ambiguous (for anybody not coming from javascript and being used to it)
- kevindqc 9y agoHow does it encourage to use extra variables? If you have a variable with the name use it, otherwise do whatever you usually do like calling a function. It doesn't encourage doing something like const id = getId(); let obj = {id, otherProp: 'something'}; vs let obj = {id: getId(), otherProp: 'something'}; ?
- bpicolo 9y agoRight. The real argument is I don't like the additional ambiguity. For me, it doesn't seem like a particularly ergonomic syntax choice.