Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>To load up modules from another modules that you <code>define()</code>, you would simply set the first parameter as an array, with your module names in it. So let's say, in your code, you wanted to load <code>Player.js</code> into <code>AssetsManager.js</code>, you would simply include the string <code>Player</code> in the array.</p> <p>This is simply possible because <code>define</code>'s abstract implementation is equivalent to <code>require</code>, only that the callback passed to <code>define</code> expects a value to be returned, and that it will add a "module" to a list of dependencies that you can load up.</p> <p><strong>AssetsManager.js</strong></p> <pre><code>define(['Player'], function (player) { //... Your code. }); </code></pre> <p>However, if I can add to it, I personally prefer the use of <code>require</code> inside of the callback passed to <code>define</code> to grab the dependency that you want to load, instead of passing parameter to the callback.</p> <p>So here's my suggestion:</p> <pre><code>define(['Player'], function () { var player = require('Player'); }); </code></pre> <p>And this is because it's much more in tune with <a href="http://en.wikipedia.org/wiki/CommonJS" rel="nofollow">CommonJS</a>.</p> <p>And this is how <code>main.js</code> would look like formatted to be more CommonJS-friendly:</p> <pre><code>require(["Player", "AssetsManager"], function () { var player = require('Player'); var manager = require('AssetsManager'); player.loadXML(); }); </code></pre> <p>But the CommonJS way of doing things is just a personal preference. My rationale for it is that the order in which you input the dependency names in the array might change at any time, and i wouldn't want to have to step through both the array and the parameters list.</p> <p>Another rationale of mine (though, it's just pedantic), is that I come from the world of Node.js, where modules are loaded via <code>require()</code>.</p> <p>But it's up to you.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload