Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save bentruyman/647655 to your computer and use it in GitHub Desktop.

Select an option

Save bentruyman/647655 to your computer and use it in GitHub Desktop.
Dynamic property names in object literals
// Can we get something like this??
var howManyRainbows = 'double';
var rainbow = {
(howManyRainbows): true
};
if (rainbow.double) {
alert('What does it mean?');
}
@bentruyman
Copy link
Copy Markdown
Author

Yep, just as @getify said, there are other ways of accomplishing this in JavaScript, but I'd like to see if anyone else finds something like what I'm doing useful.

For example:

var quxx = {
  corge: 1,
  grault: false,
  (foo): true,
  (baz): 9001
};

I'd rather construct my object in one fell swoop, instead of:

var foo = 'bar'
  , baz = 'qux';

var quxx = {
  corge: 1,
  grault: false
};
quxx[foo] = true;
quxx[baz] = 9001;

@Singles, my apologies for being vague in my question.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment