Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>While using the <code>window</code> object as a place to share functionality among different parts of your code can work quite well, it can get somewhat ugly when you're working on a big/complex codebase. Also, you have to handle loading all of the dependencies manually, which can get somewhat frustrating too. Many people just end up loading everything in every request, regardless of what's actually needed for that particular page.</p> <p>There are plenty of libraries built in an attempt to solve those issues and make exporting and importing functionality among files more managable. One of the more common ones today is <a href="http://requirejs.org/" rel="noreferrer">RequireJS</a> which is an implementation of the <a href="https://github.com/amdjs/amdjs-api/wiki/AMD" rel="noreferrer">CommonJS AMD specs</a>. You can find other libraries and a comparison between them <a href="https://spreadsheets.google.com/a/shesek.info/lv?key=tDdcrv9wNQRCNCRCflWxhYQ&amp;toomany=true#gid=0" rel="noreferrer">here</a>, and there's a great tutorial on how to write modular JavaScript using those libraries over at <a href="http://addyosmani.com/writing-modular-js/" rel="noreferrer">Addy Osmani blog</a> - which also talks about the new upcoming modules system in ES.next, which is quite interesting too.</p> <p>I personally really like <a href="http://nodejs.org/docs/v0.6.3/api/modules.html" rel="noreferrer">NodeJS's modules system</a> (with the <code>exports</code> object and the <code>require</code> function), so I use <a href="https://github.com/substack/node-browserify" rel="noreferrer">node-browserify</a> to package it up for working on the client side too. While that doesn't allow dynamically loading dependencies in an asynchronous way as the AMD specs does, it does allow to nicely share the same JavaScript code on both the client-side and server-side, which is a huge bonus for me (mainly because I'm working with NodeJS on the server side too, so I'm not sure how important that might be for you) and it handles packaging all of the dependencies together nicely for you (so they can be synchronously <code>require()</code>d) by parsing your JavaScript code and looking for plain <code>require()</code> calls to determine what a given script requires in order to run.</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