TypeScript and Angular - When to Use a Module vs. IIFE

I am just starting to use TypeScript with Angular. I go through some sample applications and sometimes I see .js

that starts with IIFE and other times the file starts with a module declaration.

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

+3


source to share


1 answer


When you write a module in TypeScript:

module MyModule {
    export class Example {

    }
}

      

The result is an expression that is called immediately:



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

      

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

+3


source







All Articles