In ES6 object literals have a number of shorthands. You can computer property names right in the declaration, define methods with a shorter syntax, and have more convinient declarations when the key matches the value:
function person(name, age, isDogLover)
{
return {
// Set the prototype, in this case null to clear it out.
__proto__: null,
// Shorthand for "name: name"
name,
// Shorthand for "age: age"
age,
// Methods
toString()
{
return "A person of age " + this.age;
},
// Computed (dynamic) property names
[ isDogLover ? "dog" : "cat" ]: { name: isDogLover ? "Spot" : "Tails" }
}
};
person("Tom", 10, true);