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…

2 comments

  • posted 3 years ago by 5fcbb66ef90325001960186e
    hybrid cars are ...
  • posted 2 years ago by paperluigis
    duck

sign in to comment