I was having a look at the JS code generated by TypeScript on this page:
http://www.typescriptlang.org/Playground/
Basically, to create a Greeter class, it outputs this:
var Greeter = (function () {
function Greeter(message) {
this.greeting = message;
}
Greeter.prototype.greet = function () {
return "Hello, " + this.greeting;
};
return Greeter;
})();
var greeter = new Greeter("world");
So I’m wondering why they are mixing the module and prototype pattern? Wouldn’t it be the same just to do:
function Greeter(message) {
this.greeting = message;
}
Greeter.prototype.greet = function () {
return "Hello, " + this.greeting;
}
var greeter = new Greeter("world");
?
The use of module pattern here is to create a closure, allowing for more control over any closed over variables (nothing leaking into global, no global pollution), and allowing for the creation of ‘private’ variables (variables that only exist via the module pattern’s closure).