Thomas Zoé Thomas Zoé - 1 month ago 39
TypeScript Question

Typescript Revealing Module Pattern

I am using The Revealing Module Pattern with JS in an angular application.

Now I am going to migrate it from Javascript to Typescript, but I have no idea how to use this pattern with TS. I have read some other post like the following, but no one of them have the return at the beginning of the function. TypeScript code similar to Revealing Module Pattern structure

angular.module('app.services')
.factory('justAService', justAService);

function justAService() {

return {
a: a
};

var x = {};

function a(){....}


Is it possible to write the return at the top?

Thank you

Answer

Yes it's possible. It's usually used to improve readability and it doesn't affect the program execution at all. Because function evaluation (definition) occur before that javascript gets executed, so when your module returns a reference to the function, it had already been defined. But it just works for functions declared this way, otherwise will be unreachable code.

Example:

module App{
    angular.module('app.services')
        .factory('justAService', justAService);

    function justAService() {

        return {
            a: a
        };

        function a() {

            var x = {};
        }
    }
}
Comments