#author("2022-07-26T02:51:04+09:00","","") [[Javascript/デザインパターン]] * Javascript/デザインパターン/Clean Code/Set default objects with Object.assign [#sa36112f] オブジェクトのデフォルト値設定 let params = { title : 'CODE COMPLETE 2nd Edition', author : 'Steve McConnell', info : { memo2 : "sample" } }; const DEFAULT_PARAMS = { title : '', author : 'unknown', isbn : '-', info : { memo : "memo", memo2 : "memo2" } }; let bookInfo = Object.assign(DEFAULT_PARAMS, params); console.log(bookInfo); // 出力 // { // title : 'CODE COMPLETE 2nd Edition', // author : 'Steve McConnell', // isbn : '-', // info : { memo2 : "sample" } // } 少し実用的な例) function handler(event) { const DEFAULT_EVENT = { httpMethod : "GET", pathParameters : { proxy : "path/to/resource" }, body : JSON.stringify({ key1 : "val1", key2 : "val2" }), queryStringParameters : { key3 : "val3", key4 : "val4" } }; let eventParams = Object.assign(DEFAULT_EVENT, event); console.log(eventParams); }