Skip to content

Instantly share code, notes, and snippets.

@siddharth-sunchu
Created June 26, 2021 23:08
Show Gist options
  • Save siddharth-sunchu/23ec1f5cacf7ca89e2812e515353c7ba to your computer and use it in GitHub Desktop.
Save siddharth-sunchu/23ec1f5cacf7ca89e2812e515353c7ba to your computer and use it in GitHub Desktop.
const employee = {
name: 'Siddharth',
age: 30,
salary: {
annual: '100K',
hourly: '$50'
}
};
const copyOfEmployee = Object.create( employee);
console.log('employee => ', employee);
console.log('copyOfEmployee => ', copyOfEmployee);
/*
employee => {
name: 'Siddharth',
age: 30,
salary: { annual: '100K', hourly: '$50' }
}
copyOfEmployee => {}
*/
console.log('------------After Modification-----------');
copyOfEmployee.name = 'Elon Musk';
copyOfEmployee.salary.annual = '120K';
/*
Here you would expect employee object wouldn't change, but copyOfEmployee
and employee object both share same memory address
*/
console.log('employee => ', employee);
console.log('copyOfEmployee => ', copyOfEmployee);
/*
------------After Modification-----------
employee => {
name: 'Siddharth',
age: 30,
salary: { annual: '120K', hourly: '$50' }
}
copyOfEmployee => { name: 'Elon Musk' }
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment