public static deepCopy<T>(instance: T): T {
if (instance == null) {
return instance
}
if (instance instanceof Date) {
return new Date(instance.getTime()) as any
}
if (instance instanceof Array) {
var cloneArr = [] as any[]
;(instance as any[]).forEach((value) => {
cloneArr.push(value)
})
return cloneArr.map((value: any) => GameUtils.deepCopy<any>(value)) as any
}
if (instance instanceof Object) {
var copyInstance = {...(instance as { [key: string]: any }),
} as { [key: string]: any }
for (var attr in instance) {
if ((instance as Object).hasOwnProperty(attr)) copyInstance[attr] = GameUtils.deepCopy<any>(instance[attr])
}
return copyInstance as T
}
return instance
}
2 个回复
小高
赞同来自:
Aimumu
赞同来自: