ES6 in Depth
  • Introduction
  • Installation
  • Block Scope
    • Constant
    • Use cases
  • Template String
    • New String Methods
    • Tagged Template Literal
  • New Number Methods
  • Arrow Function
    • Default Parameters
    • Lexical This Scope
    • Where not to use arrow function
  • Object Enhancement
  • New Array Methods
    • For Of
  • Spread Operator
  • Destructuring
    • Array Destructuring
  • Class
  • Symbols
  • New Data Structures
    • Set
    • Map
    • WeakSet
    • WeakMap
    • Iterators
    • Generators
  • Promise
  • Import Export
Powered by GitBook
On this page
  • Dynamic Object Key
  • Shorthand properties
  • Copy properties to other object - Object.assign()

Was this helpful?

Object Enhancement

var color = "red";
var speed = 10;
var drive= 'driveBMW';
fuction go(){}

var car = {color, speed, go, [drive]: function(){}}; // ES5 - {color: color, speed: speed}

console.log(car.color);
console.log(car.speed);
car.go();
car.driveBMW();

Dynamic Object Key

let key = "myColor";
let val = '#f00';

let obj = {
    [key]: val,
    [`${key}Opposite`]: val
};

console.log(obj); // {"myColor":"#f00","myColorOpposite":"#f00"}

Shorthand properties

var firstname = "Hemant";
var lastname = "Singh";

var fullname = {firstname, lastname};

var prefix = 'Mr.';

var person = {prefix, fullname};
console.log(person);

Copy properties to other object - Object.assign()

var obj1 = {
    name: "hemant",
    age: 30
};

var obj2 = {
    loc: "hyd",
    pin: 223
};

console.log(JSON.stringify(Object.assign({status: 'cool'}, obj1, obj2)));
// {"status":"cool","name":"hemant","age":30,"loc":"hyd","pin":223}

```

PreviousWhere not to use arrow functionNextNew Array Methods

Last updated 4 years ago

Was this helpful?