Destrukturizacija objekata parametara funkcija u JavaScript-u
Na sličan način možete destrukturisati parametre-objekte:
function func({year, month, day}) {
console.log(year); // ispisaće 2025
console.log(month); // ispisaće 12
console.log(day); // ispisaće 31
}
func({year: 2025, month: 12, day: 31,});
Prepravite sledeći kod preko destrukturizacije prema proučenoj teoriji:
function func(options) {
let color = options.color;
let width = options.width;
let height = options.height;
}
func( {color: 'red', width: 400, height: 500} );
Prepravite sledeći kod preko destrukturizacije prema proučenoj teoriji:
function func(options) {
let width = options.width;
let height = options.height;
let color;
if (options.color !== undefined) {
color = options.color;
} else {
color = 'black';
}
}
func( {color: 'red', width: 400, height: 500} );