Javascript Typescript:声明空类对象
是否可以从没有默认值的类中声明对象 我有一个如下所示的类Javascript Typescript:声明空类对象,javascript,typescript,object,Javascript,Typescript,Object,是否可以从没有默认值的类中声明对象 我有一个如下所示的类 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.
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;
}
我试图实现的是检查对象中是否有未定义
或空
,并返回此类
我查看了许多示例代码和文档,无论是隐式构造函数还是显式构造函数,都可以使用类名前面的
new
来声明新类,但随后它们声明了一些值。因此,我认为这些实例在被一些外部范围声明之前是不存在的。为什么不可能呢
类中的字段和属性将设置为默认值,如果您不按如下方式指定其默认值,则字符串为null:
export class Month {
January: string = "Something";
February: string = "Something";
March: string = "Something";
...
}
为什么不可能呢 类中的字段和属性将设置为默认值,如果您不按如下方式指定其默认值,则字符串为null:
export class Month {
January: string = "Something";
February: string = "Something";
March: string = "Something";
...
}
更新:
这将传输到以下JS代码:
var Foo = /** @class */ (function () {
function Foo() {
}
return Foo;
}());
var foo = new Foo();
console.log(foo.a);
您的对象没有任何键,因为它们尚未定义,所以它们使用“未定义”文本,而不是“未定义”作为值。
您可以尝试在默认值为“undefined”的行中执行以下操作:
class Foo {
a: string = undefined;
b: string = undefined;
c: number = undefined;
}
var foo = new Foo();
console.log(foo.a); //undefined
console.log(foo); //=> Foo{a: undefined, b: undefined, c: undefined}
Object.keys(foo).forEach(property => console.log(property));
// keys log as empty and not undefined
这将传输到以下JS:
var Foo = /** @class */ (function () {
function Foo() {
this.a = undefined;
this.b = undefined;
this.c = undefined;
}
return Foo;
}());
var foo = new Foo();
console.log(foo.a); //undefined
console.log(foo); //=> Foo{a: undefined, b: undefined, c: undefined}
Object.keys(foo).forEach(function (property) { return console.log(property); });
// keys log as empty and not undefined as part of the forEach loop
我个人建议不要使用这种实现,因为它容易出现意外行为,并且不利于typescript带来的静态类型的JS版本
也许如果你能更详细地介绍一下你想要实现的目标,我们可以给你一个更好的解决方案
更新:
这将传输到以下JS代码:
var Foo = /** @class */ (function () {
function Foo() {
}
return Foo;
}());
var foo = new Foo();
console.log(foo.a);
您的对象没有任何键,因为它们尚未定义,所以它们使用“未定义”文本,而不是“未定义”作为值。
您可以尝试在默认值为“undefined”的行中执行以下操作:
class Foo {
a: string = undefined;
b: string = undefined;
c: number = undefined;
}
var foo = new Foo();
console.log(foo.a); //undefined
console.log(foo); //=> Foo{a: undefined, b: undefined, c: undefined}
Object.keys(foo).forEach(property => console.log(property));
// keys log as empty and not undefined
这将传输到以下JS:
var Foo = /** @class */ (function () {
function Foo() {
this.a = undefined;
this.b = undefined;
this.c = undefined;
}
return Foo;
}());
var foo = new Foo();
console.log(foo.a); //undefined
console.log(foo); //=> Foo{a: undefined, b: undefined, c: undefined}
Object.keys(foo).forEach(function (property) { return console.log(property); });
// keys log as empty and not undefined as part of the forEach loop
我个人建议不要使用这种实现,因为它容易出现意外行为,并且不利于typescript带来的静态类型的JS版本
也许如果你能更详细地说明你想要实现的目标,我们可以给你一个更好的解决方案?我也这么认为,但正如你在我的代码中看到的,我在控制台上做了,它打印了
Month{}
而不是Month{一月:未定义,…}
。我查看了许多示例代码及其在typescript文档中的实现方式,它们总是有一个构造函数或一个传递给类的值,就像您需要在定义存在之前填充它一样。我希望这对你有意义。@Kim.LBy也许这会有所帮助:谢谢@Jovan,只要看看你的链接,看起来这就是我要找的。我也这么想,但正如你在我的代码中看到的那样,我在控制台上打印了Month{}
,而不是Month{一月:未定义,.}
。我查看了许多示例代码以及它在typescript文档中的实现方式,它们总是有一个构造函数或一个传递给类的值,就像你需要在定义存在之前填充它一样。我希望这对你有意义。@Kim.LBy也许这会有所帮助:谢谢@Jovan,只要看看你的链接,看起来这就是我要找的。对于他们试图实现的目标仍然感到模糊,可能需要第二时间来理解这一点。我对我的问题进行了更新,我希望这将为您提供更多关于我试图实现的目标的信息。如果我更新的答案带来了任何欢乐,请告诉我。我有一种感觉,您面临的问题在您的代码中有更深的根源,更具体地说是OOP原则的糟糕实现。如果某些特定月份是“特殊的”,但仍然与常规月份保持相同的类结构,但添加了“特殊”属性作为布尔值,则可以选择将month类作为扩展month类的SpecialMonth类的父级。请看以下内容:谢谢Liviu,我感谢你周到的回答,并意识到我所怀疑的是真的,我从某人那里继承了这段代码,因此我正在设法使它发挥作用。我对我的问题进行了更新,我希望这将给你更多关于我试图实现的内容。如果我更新的答案带来任何乐趣,请告诉我。我有一种感觉,您面临的问题在您的代码中有更深的根源,更具体地说是OOP原则的糟糕实现。如果某些特定月份是“特殊的”,但仍然与常规月份保持相同的类结构,但添加了“特殊”属性作为布尔值,则可以选择将month类作为扩展month类的SpecialMonth类的父级。请看以下内容:谢谢Liviu,我感谢你周到的回答,并意识到我所怀疑的是真的,我从某人那里继承了这段代码,所以我正在努力使它以某种方式工作。