-
Notifications
You must be signed in to change notification settings - Fork 257
Add additional checking for angular source #195
base: master
Are you sure you want to change the base?
Conversation
add additional checking to require `angular.js`
Update index.js
@douglasduteil |
@@ -1,6 +1,8 @@ | |||
//https://github.com/angular/angular.js/pull/10732 | |||
|
|||
var angular = require('angular'); | |||
if (typeof angular !== 'undefined') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With variable hoisting the declaration of angular will be moved to the top of the file, which makes declare angular variable and set it to undefined. So the above condition fails, you need to have a different way to check whether angular is loaded or not
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where do you see to assigning it to undefined
? It's only a conditional.
It's widely popular in npm comunity for AMD/UMD/CommonsJs/ES6module support.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am referring to Variable hoisting
"Variable declarations, wherever they occur, are processed before any code is executed. The scope of a variable declared with var is its current execution context, which is either the enclosing function or, for variables declared outside any function"
MDN Reference https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Statements/var
Please let me know if you think otherwise
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you really aware what require()
do?)
It doesn't execute anything, just returns an entity(function in our case)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And hoisting in this case would look like when interpretation be 3th
line {...window.angular: undefined}
or a function if it was required early.
In my case I load
angular.js
from cdn before import of the module.So main point is that you need to have additional checking to
require
the angular source.