JavaScript Shallow Copy (spread operator)
const myBrowser = {
language: 'JavaScript',
framework: 'Angular',
browser: 'Brave',
os: 'Windows 11',
date: {
time: new Date().getTime(),
date: null
}
}
const myBrowserShallowCopy = {...myBrowser};
console.log('before myBrowser:', myBrowser);
console.log('before myBrowserShallowCopy:', myBrowserShallowCopy);
myBrowserShallowCopy.browser = 'Chrome';
console.log('after update shallow myBrowser:', myBrowser);
console.log('after update shallow myBrowserShallowCopy:', myBrowserShallowCopy);
myBrowser.date.date = new Date();
console.log('after update original myBrowser:', myBrowser);
console.log('after update original myBrowserShallowCopy:', myBrowserShallowCopy);
no comments