gpt4 book ai didi

javascript - Object.keys(myObject) 不返回 typescript 中类的属性

转载 作者:行者123 更新时间:2023-11-30 11:08:52 25 4
gpt4 key购买 nike

为什么:

let provider = new Provider();
var keys = Object.keys(provider);
console.log(keys);

返回一个空数组,当:

let provider = { Name: '', Surname: '' };
var keys = Object.keys(provider);
console.log(keys);

返回一个包含 2 个属性的数组?

这是我的Provider.ts:

export class Provider {
public Id: string;
public Name: string;
public FriendlyName: string;
public CompanyRegistrationNumber: string;
public VatRegistrationNumber: string;
public TfgUniqueReferenceNumber: string;
public Since: Date;
public Until: Date;
public DefaultBillingDay: number;
public DefaultProvisioningDay: number;
public AllowsToProvisionProRata: boolean;
public AllowsToBillProRata: boolean;
public EmailAddress: string;
public DateCreated: Date;
public DateModified: Date;
public UserCreated: string;
public UserModified: string;
public IsRetired: boolean;
public ImageUrl: string;
public ContactNumber1: ContactNumber;
public ContactNumber2: ContactNumber;
public PhysicalAddress: Address;
public PostalAddress: Address;
public FinancialSystemAccount: FinancialSystemAccount;
public WholesaleProducts: Array<WholesaleProduct>;
public ContactPeople: Array<ContactPerson>;
public Batches: Array<Batch>;
public BatchCandidates: Array<BatchCandidate>;
public AllowableBatchTypes: Array<BatchType>;
}

最佳答案

除非您初始化这些属性,否则它们实际上不会被创建。您可以查看转发的Provider.js归档并验证。这只是告诉编译器,每当我们在 Provider 实例中使用它时的一种方式。 ,强制使用这些预定义类型。

Typescript Playground 上测试它

该类:

class Provider {
public Id: string;
public Name: string;
public FriendlyName: string;
public CompanyRegistrationNumber: string;
public VatRegistrationNumber: string;
public TfgUniqueReferenceNumber: string;
public InitializedProp: string = '';
}

被转译为:

var Provider = (function () {
function Provider() {
this.InitializedProp = '';
}
return Provider;
}());

您可以看到只有 InitializedProp在转译的构造函数中创建为属性。

关于javascript - Object.keys(myObject) 不返回 typescript 中类的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54559745/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com