如何重置以清除Typescript类中的值 [英] How to Reset clear the values in a Typescript class

查看:781
本文介绍了如何重置以清除Typescript类中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何重置Typescript中的类中的值,并确保清除所有值?有打字稿功能可以做到这一点吗?

How do I reset the values in a class in Typescript and ensure all the values are cleared? Is there a typescript function to do this?

export class Product{
   productId: number; 
   productName: string;
   productDescription: string;
}

我们有许多具有50多个字段的课程,希望能找到有效的方法来实现这一目标.

We have many classes with 50+ fields, looking for efficient way to conduct this.

推荐答案

尝试如下:

工作演示

Working Demo

productDetails: Product = {
  productId: 1,
  productName: "Apple",
  productDescription: "Fruit"
};

reset() {
  this.productDetails = new Product();
}

这篇关于如何重置以清除Typescript类中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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