1

I defined a class:

export class SavedData{
    public isDone : boolean;
}

and try to stringify it:

console.log(new SavedData());

but it doesn't include the isDone property

{}

,which I need to move isDone to constructor:

export class SavedData{
  constructor(public isDone : boolean){
  }
}

to be successful:

{"isDone":false}

why would it happen? and is it possible to stringify a class property without declaring it into constructor?

1
  • Does it work if you explicitly invoke JSON.stringify? Commented Nov 16, 2016 at 7:58

1 Answer 1

1

It's working if you initialize it with a default value

export class SavedData{
    public isDone : boolean = null; // or = false;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.