Last active
April 5, 2023 14:45
-
-
Save DevGW/d59117d8a5ed4e13dba1d8ef7ba0baf2 to your computer and use it in GitHub Desktop.
Javascript :: Object Spreads refactoring #js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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