Déstructuration des objets paramètres des fonctions en JavaScript
De la même manière, on peut déstructurer les paramètres-objets :
function func({year, month, day}) {
console.log(year); // affichera 2025
console.log(month); // affichera 12
console.log(day); // affichera 31
}
func({year: 2025, month: 12, day: 31,});
Refaites le code suivant via la déstructuration selon la théorie étudiée :
function func(options) {
let color = options.color;
let width = options.width;
let height = options.height;
}
func( {color: 'red', width: 400, height: 500} );
Refaites le code suivant via la déstructuration selon la théorie étudiée :
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} );