-
Notifications
You must be signed in to change notification settings - Fork 27
LMD require
azproduction edited this page Feb 13, 2013
·
4 revisions
This 3 lines explains how the require works in most cases. Require can work with modules as functions/module, strings and objects w/o any plugins.
function require(name) {
return cache[name] || modules[name] || window[name];
}
- if
name in cache
-
return
cache[name]
-
return
-
module
becomemodules[name]
- if
module
!undefined andmodule
is@shortcut
thenname
become string "shortcut"-
module
becomemodules[shortcut]
-
- if
module
!undefined-
module
is lazy - a string &&.startsWith('(function(')
-
module
becomeFunction('return ' + module)()
-
-
module
is callable-
module
becomemodule(require, exports, module) || module.exports
- cache
module
under keyname
-
return
module
-
-
module
is not callable- cache
module
under keyname
-
return
module
- cache
-
- if
module
undefined then returnglobals[name]
without cache