Funksiya parametr obyektlərinin destrukturizasiyasi JavaScript-də
Eyni şəkildə parametr-obyektləri də destrukturizasiya etmək olar:
function func({year, month, day}) {
console.log(year); // 2025 çap edəcək
console.log(month); // 12 çap edəcək
console.log(day); // 31 çap edəcək
}
func({year: 2025, month: 12, day: 31,});
Aşağıdakı kodu öyrənilən teoriyaya uyğun olaraq destrukturizasiya ilə dəyişin:
function func(options) {
let color = options.color;
let width = options.width;
let height = options.height;
}
func( {color: 'red', width: 400, height: 500} );
Aşağıdakı kodu öyrənilən teoriyaya uyğun olaraq destrukturizasiya ilə dəyişin:
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} );