Someone just showed me that in a JavaScript object literal, you can use a variable instead of a key-value pair. This results in a property with the variable's name as the key and the variable's value as the value:
var someProperty = "someValue";
var someObject = {
someProperty
};
//Variable name became property name
alert(someObject.someProperty); //Outputs "someValue"
I'd never heard of this syntax before, but we ran it in the Chrome console and it worked. I tested this in the following browsers, and here are the results:
- Chrome 51: works
- Firefox 38: works
- Internet Explorer 11: errors with
Expected ':'
- Edge 25: works
My questions are
- Is there a name for this feature?
- Is this standardised? If so, which standard? ES6?
- Is there documentation for this somewhere? (Such as on MDN)