const userConfig = { name: "田中太郎", age: 22 }; const additionalConfig = { age: 30, role: 1 }; Object.assign(userConfig, additionalConfig); console.log("✅ userConfig: ", userConfig); // ✅ userConfig: {name: '田中太郎', age: 30, role: 1} console.log("✅ additionalConfig: ", additionalConfig); // ✅ additionalConfig: {age: 30, role: 1}
userConfig
に結合されるconst userConfig = { username: "田中太郎", age: 22 }; const shopConfig = { shopname: "山田雑貨店", role: 1 }; const config = { ...userConfig, ...shopConfig }; console.log("✅ config: ", config); // ✅ config: {username: '田中太郎', age: 22, shopname: '山田雑貨店', role: 1}
まとめたい時
function bundleConfig() { ...色々処理 const userConfig = this.userConfig; const shopConfig = this.shopConfig; const config = { ...userConfig, ...shopConfig }; ...色々処理 return config; }