Friday, January 2, 2015

Overview

Javascript’s new version (EcmaScript 6) has some amazing new features. The one discussed in this post is enhanced properties. More information can be found here.

Examples

Property shorthands

1
2
3
let name = "Jim";
let age = 21;
let person = { name, age }; // { name: "Jim", age: 21 }

Deconstructing

1
2
3
4
let person = { name: "Jim", age: 21 };
let { name, age } = person;
console.log(name);  // Jim
console.log(age);   // 21

Computed Property Keys

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// Before
var key = "myKey";
var obj = {};
obj[key] = 5;

// Now
let key = "myKey";
let keyNum = 1;
let obj = {
  [key] = 5;        // computed property name
  [key + keyNum](){ // computed method name
    return true;
  }
};
console.log( obj["myKey"] );    // 5
console.log( obj["myKey1"]() ); // true

Random Posts