Back to questions
JuniorTheoryCommon
What does the program print?
class A {
private x = 1;
}
class B {
#x = 1;
}
console.log(JSON.stringify(new A()), JSON.stringify(new B()));{"x":1} {}
- Expecting
privateto hidexfromJSON.stringify: tsc erases the modifier, and in the JavaScriptxis an ordinary field thatJSON.stringifylists. - Expecting
#xto show up like any other field: a#field is private in JavaScript itself, so neitherJSON.stringifynorObject.keyssees it.
- What does tsc report for
new A().xand fornew B().#xwritten outside the classes? - When is
privateenough, and when do you need a#field?