Destrukturasie van funksie parameter objekte in JavaScript
Op 'n soortgelyke wyse kan jy parameter-objekte destrukturiseer:
function func({year, month, day}) {
console.log(year); // sal 2025 afdruk
console.log(month); // sal 12 afdruk
console.log(day); // sal 31 afdruk
}
func({year: 2025, month: 12, day: 31,});
Herskep die volgende kode deur destrukturasie volgens die bestudeerde teorie:
function func(options) {
let color = options.color;
let width = options.width;
let height = options.height;
}
func( {color: 'red', width: 400, height: 500} );
Herskep die volgende kode deur destrukturasie volgens die bestudeerde teorie:
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} );