title: How to handle circular dependencies with RequireJS/AMD?
body: <p>In my system, I have a number of "classes" loaded in the browser each a separate files during development, and concatenated together for production. As they are loaded, they initialize a property on a global object, here <code>G</code>, as in this example:</p>
  
  <pre><code>var G = {};
  
  G.Employee = function(name) {
      this.name = name;
      this.company = new G.Company(name + "'s own company");
  };
  
  G.Company = function(name) {
      this.name = name;
      this.employees = [];
  };
  G.Company.prototype.addEmployee = function(name) {
      var employee = new G.Employee(name);
      this.employees.push(employee);
      employee.company = this;
  };
  
  var john = new G.Employee("John");
  var bigCorp = new G.Company("Big Corp");
  bigCorp.addEmployee("Mary");
  </code></pre>
  
  <p>Instead of using my own global object, I am considering to make each class its own <a href="http://wiki.commonjs.org/wiki/Modules/AsynchronousDefinition" rel="noreferrer">AMD module</a>, based on <a href="https://stackoverflow.com/questions/4869530/requirejs-how-to-define-modules-that-contain-a-single-class/4878241#4878241">James Burke's suggestion</a>:</p>
  
  <pre><code>define("Employee", ["Company"], function(Company) {
      return function (name) {
          this.name = name;
          this.company = new Company(name + "'s own company");
      };
  });
  define("Company", ["Employee"], function(Employee) {
      function Company(name) {
          this.name = name;
          this.employees = [];
      };
      Company.prototype.addEmployee = function(name) {
          var employee = new Employee(name);
          this.employees.push(employee);
          employee.company = this;
      };
      return Company;
  });
  define("main", ["Employee", "Company"], function (Employee, Company) {
      var john = new Employee("John");
      var bigCorp = new Company("Big Corp");
      bigCorp.addEmployee("Mary");
  });
  </code></pre>
  
  <p>The issue is that before, there was no declare-time dependency between Employee and Company: you could put the declaration in whatever order you wanted, but now, using RequireJS, this introduces a dependency, which is here (intentionally) circular, so the above code fails. Of course, in <code>addEmployee()</code>, adding a first line <code>var Employee = require("Employee");</code> would <a href="http://requirejs.org/docs/api.html#circular" rel="noreferrer">make it work</a>, but I see this solution as inferior to not using RequireJS/AMD as it requires me, the developer, to be aware of this newly created circular dependency and do something about it.</p>
  
  <p>Is there a better way to solve this problem with RequireJS/AMD, or am I using RequireJS/AMD for something it was not designed for?</p>