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…

4 comments

  • posted 4 years ago by 5fcbb66ef90325001960186e
    hybrid cars are ...
  • posted 3 years ago by paperluigis
    duck
  • posted 5 months ago by 66354ae006f96200082296aa
    EVAN HERE ENHANCE MALE MUSCLES AND P
  • posted 4 months ago by hajizade1992
    hello

sign in to comment