我正在尝试从另一个脚本创建打字稿。 问题是,当我更改克隆对象的属性时,它也会同时更改源对象的属性。
示例:
import { arrayProp, ModelType, prop, staticMethod, Typegoose, instanceMethod, InstanceType } from 'typegoose';
class User extends Typegoose
{
@prop({ required: true })
name: string;
@prop({ required: true })
password: string;
...
@instanceMethod
renderSafe(this: InstanceType<User>)
{
let _return:any = null;
try
{
_return = <any>Object.create(this);
_return.password = undefined;
console.log(_return);
// prints correct object without password property
console.log(this.password);
// prints undefined
}
catch(e)
{ console.log(e); }
return _return;
}
}
是否可以克隆对象,以便结果可以存在而不影响源对象?
我也尝试过使用Object.assign({}, this);
导致相同的问题。