最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Typescript: declare empty class object - Stack Overflow

programmeradmin1浏览0评论

Is it possible to declare an object from a class with no default values?

i have a class that looks like below;

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;
}

What I am trying to achieve is checking for any undefined or null in object and return this class.

I looked at many sample codes and documentation, either you have implicit constructor or explicit constructor you can declare you new class by using the new infront of the class name, but that follows by they declare some values. So I think the instances do not exist before it is declared by some outside scope.

Is it possible to declare an object from a class with no default values?

i have a class that looks like below;

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;
}

What I am trying to achieve is checking for any undefined or null in object and return this class.

I looked at many sample codes and documentation, either you have implicit constructor or explicit constructor you can declare you new class by using the new infront of the class name, but that follows by they declare some values. So I think the instances do not exist before it is declared by some outside scope.

Share Improve this question edited May 28, 2018 at 1:10 roger asked May 27, 2018 at 23:57 rogerroger 1,2634 gold badges17 silver badges35 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4
class Foo {
  a: string;
  b: string;
  c: number;
}

var foo = new Foo();
console.log(foo.a); //undefined

update: This transpiles to the following JS code:

var Foo = /** @class */ (function () {
    function Foo() {
    }
    return Foo;
}());
var foo = new Foo();
console.log(foo.a);

Your object doesn't have any keys because they're not yet defined, so they resort to the "undefined" literal as opposed to the "undefined" as a value. You can try doing something in the lines of defaulting to the "undefined" as value:

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

this transpiles to the following 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

I would personally advise against this implementation as it is prone to unexpected behaviour and goes against the statically typed version of JS that typescript brings along.

Perhaps if you would give a bit more detail on what you're trying to achieve we could give you a better solution?

Why wouldn't be possible?

Fields and properties within a class will be set to default values, in your case null for string, if you don't specify their default values like this:

export class Month {
   January: string = "Something";
   February: string = "Something";
   March: string = "Something";
   ...
}
发布评论

评论列表(0)

  1. 暂无评论