Skip to content

Instantly share code, notes, and snippets.

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

// insted of define a new object with reference, we pass a spread operator of the original object
const newDroid = {
  ...droid,
  colors: ['blue', 'silver', 'white'],
  productionInformation: {
    manufacturer: 'Industrial Automaton',
    productLine: 'R-series',
    model: 'R2 series astromech droid'
  }
}

//the original object is not updated
console.log(droid)
// [object Object] {
//   height: '1.08',
//   name: 'R2-D2'
// }

// we created a new object with the new attributes
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