Created
June 16, 2021 02:30
-
-
Save zzpmaster/e2b988af7d0f7940b885e037a1f2a9fb to your computer and use it in GitHub Desktop.
clone object
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
function shallowCopy<T>(origin: T): T { | |
return Object.assign( | |
Object.create(Object.getPrototypeOf(origin)), | |
origin | |
) | |
} | |
function shallowCopy2<T>(origin: T): T { | |
const obj = { ...origin }; | |
Object.setPrototypeOf(obj, (origin as Object).constructor.prototype); | |
return obj; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment