Javascript ES6 way to update object properties: Object.assign
How many times did you find some code that looked like this:
And thanks to some other features of ES6, we can write the same code like this:
Note that { someVariable } is equivalent to the ES5 { someVariable: someVariable } notation.
// previous codeand you really wanted to make it look nicer? Well now you can! Introducing the ECMAScript 6 Object.assign method.
let someValue1 = 'something';
let someValue2 = 'something else';
let someValue3 = 'something blue';
let someValue4 = 'something entirely different and original';
// our code
const parentObject=new ParentObject();
parentObject.childObject.someValue1=someValue1;
parentObject.childObject.someValue2=someValue2;
parentObject.childObject.someValue3=someValue3;
parentObject.childObject.someValue4=someValue4;
And thanks to some other features of ES6, we can write the same code like this:
// our code
const parentObject=new ParentObject();
Object.assign(parentObject.childObject,{someValue1, someValue2, someValue3, someValue4});
Note that { someVariable } is equivalent to the ES5 { someVariable: someVariable } notation.
Comments
Be the first to post a comment