AngularJS : window.angular variable

血红的双手。 提交于 2020-08-08 06:47:09

问题


I was looking for informations to window.angular variable and i found nothing expect this post. In this post we have the following code :

(function(angular, undefined){
    'use script';
    var djng_forms_module = angular.module('ng.django.forms', []);
    funtion hasCode(s){
        return .....
    }
    var foo = .....
}(window.angular));

And a person explains that when we use this code fragment it ensures that it is executed after the population of the variable window.angular. I don't understand why it could not be possible that window.angular could be undefined when use this syntax, could you explain to me why please ?

Thanks in advance :)


回答1:


It has nothing to do with ensuring angular is here. It's the syntax that, as I know, does 3 things:

  1. Allows the minifier to change the names of locally scoped variables
  2. Decrease the access time to a variable, since it's locally scoped
  3. If someone would've created a locally scoped "angular" variable, you will be sure you're using the global one by explicitly using the window variable

But since the IIFE is invoked immediately, according to it's definition, it doesn't ensure in any way that window.angular is presented.



来源:https://stackoverflow.com/questions/38883889/angularjs-window-angular-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!