const user = { id: 42, isVerified: true }
// grabs the property by name in the object, ignores the order
const { isVerified, id } = user;
console.log(isVerified);
// > true
/**
* My cool function.
*
* @param {Object} obj - An object.
* @param {string} obj.prop1 - Property 1.
* @param {string} obj.prop2 - Property 2.
*/
const fn = function ({prop1, prop2}) {
// Do something with prop1 and prop2
}
const hero = {
name: 'Batman',
realName: 'Bruce Wayne',
address: {
city: 'Gotham'
}
};
// Object destructuring:
const { realName, address: { city } } = hero;
city; // => 'Gotham'
// Noob [ not good ]
function getFullName(userObj) {
const firstName = userObj.firstName;
const lastName = userObj.lastName;
return `${firstName} ${lastName}`;
}
// master [ yap little bit ]
function getFullName(userObj) {
const { firstName, lastName } = userObj;
return `${firstName} ${lastName}`;
}
// hacker [ i prefer this way ]
function getFullName({ firstName, lastName }) {
return `${firstName} ${lastName}`;
}
// example func call
getFullName({
firstName: 'John',
lastName: 'Duo'
});
({x: oof.x, y: oof.y} = foo);
/**
* My cool function.
*
* @param {Object} obj - An object.
* @param {string} obj.prop1 - Property 1.
* @param {string} obj.prop2 - Property 2.
*/
const fn = function ({prop1, prop2}) {
// Do something with prop1 and prop2
}
let person = {
firstName: 'John',
lastName: 'Doe'
};
Code language: JavaScript (javascript)