Say I have a class such as:
class MyClass {
private readonly prop1 = "prop1"
private readonly prop2: string
constructor(
prop2 = "defaultProp2",
private readonly prop3 = "defaultProp3"
) {
this.prop2 = prop2
}
...
}
What's the variables initialization order?
If you compile to ES5
you can see the order things are initialized in when the code is down compiled (and the order is consistent when native classes are used).
var MyClass = /** @class */ (function () {
function MyClass(prop2, prop3) {
if (prop2 === void 0) { prop2 = "defaultProp2"; }
if (prop3 === void 0) { prop3 = "defaultProp3"; }
this.prop3 = prop3;
this.prop1 = "prop1";
this.prop2 = prop2;
}
return MyClass;
}());
So the order is:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With