Here is the typescript code and the corresponding JavaScript that is generated. Why did microsoft choose to use this.greeting instead of using a var and hiding the variable from accessing from outside in the generated js code? changing the property to private in typescript only throws compilation error, but the generated js code is ditto
//typescript code
class Greeter {
greeting: string; //why is this not private by default???
constructor(message: string) {
this.greeting = message;
}
greet() {
return "Hello, " + this.greeting;
}
}
let greeter = new Greeter("world");
console.log(greeter.greeting); //why the heck is 'greeting accessible'
//Generated javascript as follows
var Greeter = (function () {
function Greeter(message) {
this.greeting = message; //this should have been a var ????
}
Greeter.prototype.greet = function () {
return "Hello, " + this.greeting;
};
return Greeter;
}());
var greeter = new Greeter("world");
console.log(greeter.greeting); //why the heck is 'greeting accessible'