问题
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:
- Allows the minifier to change the names of locally scoped variables
- Decrease the access time to a variable, since it's locally scoped
- 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