question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Computed object keys produce very odd output

See original GitHub issue

Hi, 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:open
  • Created 5 years ago
  • Reactions:10
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

6reactions
lauraharkercommented, May 2, 2018

Sent out a fix for internal review. I’d estimate it will be in the May release.

2reactions
lauraharkercommented, Nov 26, 2018

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found