object.assign不复制功能 [英] object.assign does not copy functions

查看:118
本文介绍了object.assign不复制功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有某些功能的通用打字稿类。

I have a common typescript class with some functions.

当我执行 this.selected = Object.assign({}作为AssignedTestType时, newTestType);

所选实例不拥有中的功能键入AssignedTestType

the selected instance does not own that functions which are in the type AssignedTestType.

如何使用object.assign制作深层副本?还是其他替代方法?

How can I make a 'deep' copy with object.assign? or any alternative method?

推荐答案


object.assign不复制函数

object.assign does not copy functions

那是不正确的

let x = {a: y => y * 2}
let z = Object.assign({}, x)
console.log(z.a(5)); // 10


所选实例

the selected instance does not own that functions which are in the type AssignedTestType.

现在,此部分为true。 Object.assign 仅会进行浅表复制,并且只会枚举自己的道具

Now this part is true. Object.assign will only do a shallow copy and will only enumerate own props

这篇关于object.assign不复制功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆