Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The dependency array format:</p> <pre><code>define(["a", "b", "c"], function (a, b, c) { }); </code></pre> <p>can indeed be annoying and error-prone. Matching up the array entries to function parameters is a real pain.</p> <p>I prefer the <code>require</code> format (<a href="http://requirejs.org/docs/api.html#cjsmodule" rel="nofollow noreferrer">"Simplified CommonJS Wrapper"</a>):</p> <pre><code>define(function (require) { var a = require("a"); var b = require("b"); var c = require("c"); }); </code></pre> <p>This keeps your lines short and allows you to rearrange/delete/add lines without having to remember to change things in two places.</p> <p>The latter format will not work on PS3 and older Opera mobile browsers, but hopefully you don't care.</p> <hr> <p>As for why doing this instead of manually namespacing objects, @peller's answer gives a good overview of why modularity is a good thing, and <a href="https://stackoverflow.com/questions/9164326/whats-the-benefit-of-the-dojo-1-7-amd-framework/9165074#9165074">my answer to a similar question</a> talks about why AMD and module systems as a way of achieving modularity are a good thing.</p> <p>The only thing I would add to @peller's answer is to expand on "paying attention to dependencies actually makes for much better code." If your module requires too many other modules, that's a bad sign! We have a loose rule in our 72K LOC JavaScript codebase that a module should be ~100 lines long and require between zero and four dependencies. It's served us well.</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