Wednesday 25 March 2020

Javascritp ES6 syntax, object destructing, functions

Object Deconstructing

const abc = Object.abc;
const def = Object.def;
It's a syntatically terse way of extracting properties from objects, into variables.
// you can rewrite this
const name = app.name;
const version = app.version;
const type = app.type;

// as this
const { name, version, type } = app;

Assign to new Variable
A property can be unpacked from an object and assigned to a variable with a different name than the object property.
const o = {p: 42, q: true};
const {p: foo, q: bar} = o;
 
console.log(foo); // 42 
console.log(bar); // true
Functions
const arrow = number => number + 1;

|||

const arrow = (number) => number + 1;

|||    

const arrow = (number) => ( number + 1 );

|||

const arrow = (number) => { return number + 1 };
https://stackoverflow.com/questions/39629962/arrow-function-without-curly-braces
https://stackoverflow.com/questions/33798717/javascript-es6-const-with-curly-braces
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment

No comments:

Post a Comment