سؤال

Maybe I've missed something specific to angular or javascript that requires this but could someone explain if there's a techical reason CarWashService is defined twice below?

(function() {

angular.module("cw.services")
    .factory("CarWashService", ['$sce', '$rootScope', function ($sce, $rootScope) {

        var CarWashService;

        return new (CarWashService = (function () {
            function CarWashService() {
                this.results = [];
                this.resultsCountText = "";
                this.PageIndex = 0;
                this.processing = false;
                this.initialized = false;
                this.showNoResults = false;
                this.showResults = false;
                this.noCarWashMessage = $sce.trustAsHtml($rootScope.resources.landingNoCarWashMessage);
            }

            return CarWashService;

        })());

    }]);

}).call(this);

What could be throwing me off is calling return new on the same line as CarWashService is being assigned. For the javascript gurus out there would it make sense to use a different name other than CarWashService in the IIFE, for readability if nothing else? Or is this an accepted pattern when doing IIFEs?

هل كانت مفيدة؟

المحلول

Whoever wrote that just made it needlessly complex, there is absolutely no benefit from just declaring CarWashService because they aren't making use of the additional closure that they've created. Even if they did need a closure for private state, they have the one from the outer function.

As a side note, this also looks incorrect for angular. If you really wanted a factory, this should probably be returning the constructor rather than an instance of it. This looks like it should be declared as a service.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top