I've been playing with TypeScript, and I notice it follows a particular style with JS.
The following TypeScript:
class Greeter {
greeting: string;
constructor(message: string) {
this.greeting = message;
}
greet() {
return "Hello, " + this.greeting;
}
}
Generates JS like this:
var Greeter = (function () {
function Greeter(message) {
this.greeting = message;
}
Greeter.prototype.greet = function () {
return "Hello, " + this.greeting;
};
return Greeter;
})();
What's the benefit of the IIFE? Why do I need to attach my methods to my prototype in a new scope?
Do I lose anything if I continue to do:
function Greeter(message) {
this.greeting = message;
}
Greeter.prototype.greet = function () {
return "Hello, " + this.greeting;
};
(as I have always done) instead?