为什么有两种不同的角函数包装方法,哪一种最好?

Why are there 2 different angular function wrapping approaches and which one is best.?

本文关键字:方法 哪一种 包装 函数 两种 为什么      更新时间:2023-09-26

所以我是javascript和angularjs的新手。但让我困惑的是:

函数

(function (angular) {
    var AuthenticationService = function($http, $cookieStore, $rootScope, $timeout,UserService) {
    }
    AuthenticationService.$inject = ['$http', '$cookieStore', '$rootScope', '$timeout','UserService'];
    angular.module("app.AuthenticationService").factory("AuthenticationService", AuthenticationService);
})(angular);

我用这个开始了一些测试示例,在app.js中,我需要做这样的事情:

(function(angular) {        
    angular.module("app.AuthenticationService", ['ngCookies']);
    angular.module("app", ['ngRoute','ngResource',"app.AuthenticationService"]);
}(angular));

函数B

(function () {
    'use strict';
    angular
        .module('app')
        .factory('AuthenticationService', AuthenticationService);
    AuthenticationService.$inject = ['$http', '$cookieStore', '$rootScope', '$timeout', 'UserService'];
    function AuthenticationService($http, $cookieStore, $rootScope, $timeout, UserService) {
    }
})();

现在我没有实用的函数B方法,但我对哪种是更好的实用方法感到困惑,为什么?

两种方法都可以防止AuthenticationServicewindow中被定义

然而,除了estus的回答中提到的缩小之外,将angular作为Function A的参数没有意义。

函数B更好,因为传递angular是不必要的,可能会混淆其他人