Created
December 14, 2018 11:48
-
-
Save sharmaabhinav/4fc66316d78dae9c447cbb7366069d07 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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