Skip to content

Instantly share code, notes, and snippets.

@DevGW
Last active April 5, 2023 14:45
Show Gist options
  • Save DevGW/d59117d8a5ed4e13dba1d8ef7ba0baf2 to your computer and use it in GitHub Desktop.
Save DevGW/d59117d8a5ed4e13dba1d8ef7ba0baf2 to your computer and use it in GitHub Desktop.
Javascript :: Object Spreads refactoring #js
// create clone of an object
function cloneMachine(parent) {
let clonedName = `${parent.name}Clone`
let clone = {
name: `${parent.name}Clone`,
species: parent.species,
offspring: []
};
parent.offspring.push(clonedName);
return clone;
}
// refactor using spreads
function cloneMachine({ name, species, offspring }) {
let clonedName = `${name}Clone`;
let clone = {
name: clonedName,
species: species,
offspring: []
};
offspring.push(clonedName);
return clone;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment