ES6 Enhanced Object Literals

node v0.12.18
version: 2.0.0
endpointsharetweet
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);
Loading…

no comments

    sign in to comment