javascript - How does the simplied commonJS wrapper work in require.js under the hood? -


consider example website

define(function (require) {     var foo = require('foo');      //define module exporting function     return function () {         foo.dosomething();     }; }); 

my question is, 'foo' loaded asynchronously, how javscript below not execute before has loaded?

this explained in http://requirejs.org/docs/api.html#cjsmodule , http://requirejs.org/docs/whyamd.html#sugar.

require.js @ point (before running function) @ string representation of function, find require calls determine dependencies , load them.

to make easier, , make easy simple wrapping around commonjs modules, form of define supported, referred "simplified commonjs wrapping":

define(function (require) {     var dependency1 = require('dependency1'),         dependency2 = require('dependency2');      return function () {}; }); 

the amd loader parse out require('') calls using function.prototype.tostring(), internally convert above define call this:

define(['require', 'dependency1', 'dependency2'], function (require) {     var dependency1 = require('dependency1'),         dependency2 = require('dependency2');     return function () {}; }); 

this allows loader load dependency1 , dependency2 asynchronously, execute dependencies, execute function.


Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

css - Can I use the :after pseudo-element on an input field? -