Skip to content

Instantly share code, notes, and snippets.

@sharmaabhinav
Created December 14, 2018 11:48
Show Gist options
  • Save sharmaabhinav/4fc66316d78dae9c447cbb7366069d07 to your computer and use it in GitHub Desktop.
Save sharmaabhinav/4fc66316d78dae9c447cbb7366069d07 to your computer and use it in GitHub Desktop.
class Counter {
constructor() {
this.counter = 2
this.info = {name: 'abhinav', age: 29}
}
add () {
this.counter += 1
}
copy () {
var copy = new Counter()
Object.keys(this).forEach((key) => {
switch(typeof this[key]) {
case 'object':
copy[key] = JSON.parse(JSON.stringify(this[key]))
break
default:
copy[key] = this[key]
}
})
return copy
}
}
var counter1 = new Counter()
var counter2 = counter1.copy()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment