本文介绍了是否角分配本身`window.angular`全球范围内,当为CommonJS的模块加载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我进口角1.4 WebPACK中的我的切入点模块中建立一个CommonJS的模块(即 VAR角=要求(角); ),但不知它成为在浏览器中的全局命名空间提供(即作为 window.angular )。

I'm importing Angular 1.4 in my entry point module of Webpack build as a CommonJS module (i.e. with var angular = require("angular");), but somehow it becomes available in global namespace of the browser (i.e. as window.angular).

在折角1.4源$ C ​​$ C,我发现下面几行:

In the source code of Angular 1.4 I found the following lines:

(function(window, document, undefined) {'use strict';

...

var
    msie,             // holds major version number for IE, or NaN if UA is not IE.
    jqLite,           // delay binding since jQuery could be loaded after us.
    jQuery,           // delay binding
    slice             = [].slice,
    splice            = [].splice,
    push              = [].push,
    toString          = Object.prototype.toString,
    getPrototypeOf    = Object.getPrototypeOf,
    ngMinErr          = minErr('ng'),

    /** @name angular */
    angular           = window.angular || (window.angular = {}),
    angularModule,
    uid               = 0;

所以,我得到它的权利,即在要求,这一行:

angular           = window.angular || (window.angular = {})

检查,如果全球角度对象可用,并且如果不是的话,将创建它。因此,角悄悄引入了副作用?

checks, if global angular object is available and if it is not, creates it. So, angular silently introduces side-effect?

推荐答案

角强烈依赖于全局变量,以及一堆其它传统图书馆是没有机会成为可作为完全成熟的CJS /在某一点(和角度的,为V1的。 5.2)。

Angular is strongly tied to global variables, as well as a bunch of other legacy libraries that didn't have a chance to become available as full-grown CJS/UMD modules at some point (and Angular still isn't, as of v1.5.2).

var
...
angular           = window.angular || (window.angular = {})

等于

if (!window.angular)
  window.angular = {};

var
...
angular           = window.angular;

前者是几个字节小,一对夫妇丹斯hackier的。

The former is several bytes smaller and a couple of dans hackier.

这篇关于是否角分配本身`window.angular`全球范围内,当为CommonJS的模块加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 13:29