JS: ES2015 Object Literal Expression Extensions

By Xah Lee. Date: . Last updated: .

New in ES2015.

Computed Property Key

obj = {[expr]:value}

Eval expr and take result as key.

const obj = {["a" + "b"]: 4 };

console.log ( obj.ab === 4 )

This is useful when you want a variable's value to be the property key.

const x = "cc";
const obj = {[x]: 3};
console.log ( obj.cc === 3 )

This new syntax is especially useful for property key that is symbol. [see JS: Symbol Tutorial]

Method Syntax Shorthand

obj = { fname() {body} }

is equivalent to

obj = { fname: function () {body}}

const obj = {
    f(x) { return x + 1; },
    g:4
};

console.log( obj.f(2) === 3 );

JS Object and Inheritance

  1. Object Overview
  2. Object Type
  3. Find Object's Type
  4. Prototype Chain
  5. Create Object
  6. Object Literal Expr
  7. Create Object with Parent X
  8. Get/Set Parent
  9. Show Prototype Chain
  10. Prevent Adding Property
  11. Clone Object
  12. Test Object Equality
  13. Add Method to Prototype

Like it? Help me by telling your friends. Or, Put $5 at patreon.

Or, Buy JavaScript in Depth

If you have a question, put $5 at patreon and message me.

Web Dev Tutorials

  1. HTML
  2. CSS
  3. JavaScript
  4. JS Obj Ref
  5. DOM
  6. SVG
  7. Blog