[JS] Object Copy
Object.assign(target, source) returns copied object from source object.
const target = { a: 1, b: 2 };
const source = { b: 4, c: 5 };
const returnedTarget = Object.assign(target, source);
console.log(target);
// expected output: Object { a: 1, b: 4, c: 5 }
console.log(returnedTarget);
// expected output: Object { a: 1, b: 4, c: 5 }
// Test clone
var params = {};
params.deliverCountry = '1101';
params.deliverAddr = "Test Street"
params.deliverCity = "Sydney"
params.deliverState = "NSW"
params.deliverPostCd = "2020"
params.postFee = 8.5
const secondObj = {};
secondObj.insUid = "T23942934"
secondObj.insDate = "Today"
secondObj.postFee = 10
console.log(params);
params = Object.assign(params, secondObj);
console.log(params);
const thirdObj = {};
thirdObj.mailForm = "Y"
thirdObj.email= "test@gmail.com"
params = Object.assign(params, thirdObj);
console.log(params)
Comments