Used to extract values from arrays and objects and assign to variables, making it easier for callers to see what they need to pass in to the function
"use strict";
// old way:
function PersonOld(options) {
this.name = options.name;
this.age = options.age;
this.height = options.height;
}
// new way:
function PersonNew({ name, age, height }) {
this.name = name;
this.age = age;
this.height = height;
}
// if you want it optional, then set to a default value:
function PersonOptional({ name, age, height } = { name: "Bill", age: 31, height: 23 }) {
this.name = name;
this.age = age;
this.height = height;
}
// you can use defaults within the object:
function PersonDefaults({ name, age = 99, height }) {
this.name = name;
this.age = age;
this.height = height;
}
var options = {name: "John", age: 34, height: 72};
var personOld = new PersonOld(options);
var personNew = new PersonNew(options);
// same output
console.log(personOld);
console.log(personNew);
console.log(new PersonOptional());
console.log(new PersonDefaults({name: "Barry", height: 53}));
Browser Support
No IE support, requires a real browser