Javascript object cloning
Thu Feb 23 2023 03:52:57 GMT+0000 (Coordinated Universal Time)
Saved by
@Chidinma
const restaurant = {
name: 'Chicken Republic',
openingHours: {
thurs: {
open: 12,
close: 22,
},
},
};
const newRestaurant = { ...restaurant };
const newRestaurant2 = Object.assign({}, restaurant);
newRestaurant.name = 'Mega Chicken';
newRestaurant2.name = 'Mega chicken';
newRestaurant.openingHours.thurs.open = 8;
newRestaurant2.openingHours.thurs.open = 8;
console.log(restaurant); //{ name: 'Chicken Republic', openingHours: { thurs: { open: 8, close: 22 } }}
console.log(newRestaurant); //{name: 'Mega Chicken', openingHours: { thurs: { open: 8, close: 22 } } }
console.log(newRestaurant2); //{name: 'Mega Chicken', openingHours: { thurs: { open: 8, close: 22 } } }
content_copyCOPY
Comments