Computed object keys produce very odd output
See original GitHub issueHi, I’m seeing some strange-looking output when using square brackets to quote object keys.
const a = { ['foo']: 1};
const b = { ['bar']: 2};
console.log(a, b);
becomes:
var a={},b=(a.foo=1,a),c={},d=(c.bar=2,c);console.log(b,d);
Appspot link here. It gets worse: more intermediary objects are created as you add more keys.
I’d expect the result to be closer to the setup without []
, i.e.:
console.log({foo:1},{bar:2});
Further context: this came about as we’re looking to use Prettier to format code in our project, but can’t easily do so as it unquotes keys that don’t strictly need to be quoted. This breaks instances where we need quoted keys not to be mangled by Closure Compiler.
One of the workaround people are using is { ['foo']: bar }
syntax, which Prettier won’t unquote, but that produces bad output when fed into CC.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:10
- Comments:8 (5 by maintainers)
Top Results From Across the Web
Vue.js computed properties, filter by object key - Stack Overflow
What I would like to do is a Vue. js computed property to filter this object by Keys and create arrays that contain...
Read more >Computed Property Names in JavaScript - ui.dev
In this post you'll learn how to have an expression be computed as a property name on an object in JavaScript using ES6's...
Read more >Computed property maybe a bug or my misunderstanding
I have a component, that receive an array of objects as a props. I have two computed property (sortedByMetro and sortedByAvailable) in order...
Read more >Dynamic JavaScript Object Keys | Hackmamba
Moving on to computed keys, the value of object keys can be computed directly in the object literal using the same bracket notation...
Read more >Invoking a component with a computed property argument ...
Define computed properties using extend() or reopen() before calling create().” What's weird is that in this case the error is coming from a ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Sent out a fix for internal review. I’d estimate it will be in the May release.
Sorry I haven’t updated this in a while. Thanks for the numbers. After some internal review, we do still want to fix this, but in a way slightly more complicated than my initial proposal to deal with the restriction on duplicate object literal keys in ES5 strict mode.
Filed Google internal bug b/120031769.