【发布时间】:2018-11-06 12:40:35
【问题描述】:
是否可以从没有默认值的类中声明对象?
我有一个如下所示的类;
export class Month {
January: string;
February: string;
March: string;
...
}
mapMyPayload(specialMonths: Birthdays) {
let myMonth = new Month;
console.log(myMonth); //=> Month {}
// here I expect the Object.keys(myMonth)
// returns {January=undefined, February=undefined, ...}
// since I assume the new Month
// declares the myMonth as an object with undefined as its value
Object.keys(myMonth).forEach(m => {
// iterate thru the keys
// check if the month exists inside the specialMonths
// and is not null or undefined
if (specialMonths[m] != null) {
myMonth[m] = specialMonths[m];
);
return myMonth;
}
我想要实现的是检查对象中的任何undefined 或null 并返回此类。
我查看了许多示例代码和文档,无论您有隐式构造函数还是显式构造函数,您都可以使用类名前面的 new 来声明新类,但随后它们会声明一些值。所以我认为这些实例在被一些外部范围声明之前是不存在的。
【问题讨论】:
标签: javascript typescript object