angular.module和window.angular.module有什么区别吗?

3
我正在进行以下代码审查:
(function () {
    'use strict';

    window.angular
          .module('moduleName')
          .directive('DirectiveName', function () {
            return {
                restrict: 'AE',
                templateUrl: '/app/moduleName/list/template.html',
                controller: 'someController',
                controllerAs: 'someCtrl',
            }
        });
}());

我一直在使用
angular
    .module('myModule')
    //etc

而不是从window.angular开始。

我认为angular是针对窗口范围的,那么这两种语法之间是否有任何真实的、实际的区别呢?

3个回答

4
任何全局定义的变量都会附加到窗口上。因此,实际上没有任何实质性的区别。

4

完全不是。在浏览器中,窗口是全局对象。

如此写道- https://developer.mozilla.org/zh-CN/docs/Web/API/Window/window

In web pages, the window object is also a global object. This means that:

  • global variables of your script are in fact properties of window:

    var global = {data: 0};
    alert(global === window.global); // displays "true"
    
  • you can access built-in properties of the window object without having to type window. prefix:

    setTimeout("alert('Hi!')", 50); // equivalent to using window.setTimeout.
     alert(window === window.window); // displays "true"
    

3
据我所知,除了在语法上和明确声明之外,我没有看到任何真正的性能差异。
然而,如果你使用window.angular而不是angular,我会选择前者,因为它更具有意图。从可读性的角度来看,这样做会更好,也更加清晰。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接