Direct assignment: By assigning a reference to an object to another variable, they point to the same object. When one variable changes the object, the other variable also reflects this change.
const obj1 = {
key: 'value' };
const obj2 = obj1;
Object Spread Operator (Spread Operator): By using the spread operator..., you can create a new object and copy all the properties of another object into the new object.
const obj1 = {
key1: 'value1' };
const obj2 = {
...obj1 };
Object.assign() method: This method is used to copy the properties of one or more source objects to the target object. It can also be used to create a new object.
const obj1 = {
key1: 'value1' };
const obj2 = Object.assign({
}, obj1);
Manual attribute copying: By traversing the attributes of the source object, they are copied to the target object one by one.
const obj1 = {
key1: 'value1' };
const obj2 = {
};
for (let key in obj1) {
if (obj1.hasOwnProperty(key)) {
obj2[key] = obj1[key];
}
}