Wrapping code in anonymous closures - good practice?


#1

Hi there!

In an Ionic project we are wrapping code from each js file in an anonymous closure like this:

(function () {
    'use strict';

    angular
            .module('demo')
            .controller('HomeController', HomeController);

    /* @ngInject */
    function HomeController(ENV) {
        var vm = this;

        // vm variables
        vm.environment = ENV.name;

        // vm functions
        vm.createGroup = createGroup;
        vm.openMyProfileModal = openMyProfileModal;

        _activate();

        /////////////////

        function _activate() {

        }

        function createGroup() {

        }

        function openMyProfileModal() {

        }
    }
})();

I’d like to ask if this is being seen by the community as good practice.


#2

This is considered general good angular practice. I’m assuming the people who wrote this app followed the johnpapa style guide…which is a very good guide. Here is the exact section addressing your question: https://github.com/johnpapa/angular-styleguide/blob/master/a1/README.md#iife


#3

Thanks @rlouie for your reply, in the meantime, after some more reading, I am absolutely convinced that this is a good practice.