czuroski
czuroski

Reputation: 4332

TypeScript and Angular - when to use a module vs an IIFE

I'm just starting out using TypeScript with Angular. I am looking through some sample applications, and sometimes I see a .js that starts with an IIFE, while other times the file starts with a module declaration.

Can anyone explain when one of these should be used over the other?

Upvotes: 3

Views: 1569

Answers (1)

Fenton
Fenton

Reputation: 251282

When you write a module in TypeScript:

module MyModule {
    export class Example {

    }
}

The result is an immediately invoked function expression:

var MyModule;
(function (MyModule) {
    var Example = (function () {
        function Example() {
        }
        return Example;
    })();
    MyModule.Example = Example;
})(MyModule || (MyModule = {}));

So you can use modules in TypeScript to make your code less noisy.

Upvotes: 4

Related Questions