Skip to content

Instantly share code, notes, and snippets.

@darleykrefta
Created March 20, 2021 13:24
Show Gist options
  • Save darleykrefta/4be553f584b8737d2c6bf41d380051f0 to your computer and use it in GitHub Desktop.
Save darleykrefta/4be553f584b8737d2c6bf41d380051f0 to your computer and use it in GitHub Desktop.
const droid = {
  name: 'R2-D2',
  height: '1.08'
}

// define newDroid with droid reference
const newDroid = droid

// add some new attributes
newDroid.colors = ['blue', 'silver', 'white']
newDroid.productionInformation = {
  manufacturer: 'Industrial Automaton',
  productLine: 'R-series',
  model: 'R2 series astromech droid'
}

// the original object is updated because the reference of newDroid is the same
console.log(droid)
// [object Object] {
//   colors: ['blue', 'silver', 'white'],
//   height: '1.08',
//   name: 'R2-D2',
//   productionInformation: [object Object] {
//     manufacturer: 'Industrial Automaton',
//     model: 'R2 series astromech droid',
//     productLine: 'R-series'
//   }
// }

// newDroid was updated by the reference
console.log(newDroid)
// [object Object] {
//   colors: ['blue', 'silver', 'white'],
//   height: '1.08',
//   name: 'R2-D2',
//   productionInformation: [object Object] {
//     manufacturer: 'Industrial Automaton',
//     model: 'R2 series astromech droid',
//     productLine: 'R-series'
//   }
// }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment