RequireJS, Circular Dependencies and Exports “Magic” Method

前端 未结 1 1095
南笙
南笙 2021-01-28 21:19

I\'ve been trying to get RequireJS set up to handle circular dependencies using the special \'exports\' magic module as recommended by James Burke\'s answer to this question.

相关标签:
1条回答
  • 2021-01-28 22:15

    Edit: I couldn't find more info about the magic exports method. I could, however, mimic its intended behavior with a dummy "Container" module. See it in this fiddle: http://jsfiddle.net/amenadiel/a7thxz98/

    console.log("start");
    
    define("Container",function() {
        var Container={};
        return Container;
    });
    
    
    define("Employee", ["Container"], function(Container) {
        var Employee= function(name) {
            this.name = name;
            this.company = new Container.Company(name + "'s own company");
        };
        Container.Employee = Employee;
    });
    
    define("Company", ["Container"], function(Container) {
        var Company=function(name) {
            this.name = name;
            this.employees = [];
        };
        Company.prototype.addEmployee = function(name) {
            var employee = new Container.Employee(name);
            this.employees.push(employee);
            employee.company = this;
        };
        Container.Company = Company;
    });
    
    define("main", ["Container","Employee","Company" ], function ( Container) {
        var john = new Container.Employee("John");
        var bigCorp = new Container.Company("Big Corp");
        bigCorp.addEmployee("Mary");
        console.log(bigCorp);
    });
    
    require(["main"]);
    
    0 讨论(0)
提交回复
热议问题