It doesn't need to explicitly mention them in the compiled JavaScript because it is a dynamic language that allows the property to be added when they are first used, for example when you set the value...
Because the property is static, you should access it via the class name. In the example below, User._name
.
class User {
private static _name: string;
static setName(name: string) {
User._name = name;
}
static getName() {
return User._name;
}
}
User.setName('Steve');
alert(User.getName()); // 'Steve'
As I mentioned, there is no need to "set up" the fact that there will be a _name
property because JavaScript doesn't require that. Here is the output from the above example:
var User = (function () {
function User() {
}
User.setName = function (name) {
User._name = name; // this adds a property if it doesn't exist
};
User.getName = function () {
return User._name;
};
return User;
})();
User.setName('Steve');
alert(User.getName());
If you want it to appear, you need to assign a value at the outset:
private static _name = '';