TIL Deconstructing Objects
POSTED ON:
TAGS: javascript codepen objects
Rather than create a big list of parameters in your function, pass the whole object and deconstruct it.
function getItem(args) {
const {price, quantity, name, type} = args;
const total = price * quantity;
return `For the ${type} ${name}: you're getting ${quantity} ${name} at $${price} each. Final price is $${total}`
}
const buyingBananas = getItem({
name: 'bananas',
price: 10,
quantity: 1,
type: 'fruit'
});
console.log(buyingBananas);
See the Pen by rockykev (@rockykev) on CodePen.
REFERENCE:
Tip #5 in 7 Little Big JavaScript Techniques That Makes Your Code Better
Related TILs
Tagged: javascript