JavaScriptにおける関数パラメータオブジェクトの分割代入
同様に、パラメータオブジェクトも分割代入できます:
function func({year, month, day}) {
console.log(year); // 2025が出力される
console.log(month); // 12が出力される
console.log(day); // 31が出力される
}
func({year: 2025, month: 12, day: 31,});
学んだ理論に基づき、以下のコードを分割代入で書き直してください:
function func(options) {
let color = options.color;
let width = options.width;
let height = options.height;
}
func( {color: 'red', width: 400, height: 500} );
学んだ理論に基づき、以下のコードを分割代入で書き直してください:
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} );