什么是垃圾收集的正确方法?
我有一些打字稿代码,其中一个变量为某种类型,无法分配null
nor undefined
。如果我想重置其值,以便可以收集上一个的垃圾,将其强加于未定义的
而不是一个空对象是有意义的吗?
type SomeType = { someProp?: number };
type MyType = { myProp: SomeType };
let variable: MyType = { myProp: { someProp: 5 } };
// What's the proper way to garbage collect?
// This
variable = { myProp: {} };
// Or this?
variable = {} as MyType;
// Or this?
variable = undefined as unknown as MyType;
I have some TypeScript code where a variable is of a certain type and can't be assigned null
nor undefined
. If I want to reset its value, so that the previous one can be garbage collected, would it make sense to force it to undefined
rather than an empty object, which is memory allocated for nothing?
type SomeType = { someProp?: number };
type MyType = { myProp: SomeType };
let variable: MyType = { myProp: { someProp: 5 } };
// What's the proper way to garbage collect?
// This
variable = { myProp: {} };
// Or this?
variable = {} as MyType;
// Or this?
variable = undefined as unknown as MyType;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论