如何识别requireJS功能? - Javascript

时间:2016-08-29 22:58:18

标签: javascript oop requirejs

我多次尝试过针对cicular依赖的requireJS。我知道循环依赖有不同的方法,我在stackoverflow中读取Q / A,但我没有。我的代码在下面,我得到以下错误。如何解决这个错误?我在这里试过this method。提前致谢。

错误:

  

未捕获TypeError:require(...)。fullNameAll不是函数

main.js

    define(["require","Employee", "Company"], function (require, Employee, Company) {
   require("Company").fullNameAll();

    });

Employee.js

define(["require", "Company"], function(require, Company) {
    function Employee(firstName, lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    Employee.prototype.fullName = function() {
        console.log("I'm here");
        require("Company").test();
    };

    return Employee;
});

Company.js:

define( ["require", "Employee"], function(require, Employee) {
    function Company(firstName, lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    Company.prototype.test = function() {

    console.log("test");
    };

    Company.prototype.fullNameAll = function() {
        var Employee = require("Employee");
        Employee.fullName();

    };
    return Company;
});

1 个答案:

答案 0 :(得分:0)

我用“出口”解决了我的问题。 解决方案:

main.js

define(["exports", "Company"], function (exports, Company) {
    var x = new Company.Company("hamdi", "bayhan");
    x.fullNameAll();
});

Employee.js

define(["exports", "Company"], function(exports, Company) {
    function Employee(firstName, lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    Employee.prototype.fullName = function() {
        console.log("I'm here");
        var c = new Company.Company("qwe","tyu");
        c.test();
    };

    exports.Employee =  Employee;
});

Company.js

define( ["exports", "Employee"], function(exports, Employee) {
    function Company(firstName, lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    Company.prototype.test = function() {

        console.log("test");
    };

    Company.prototype.fullNameAll = function() {
        var e = new Employee.Employee("asd","dsa");
        e.fullName();

    };
    exports.Company = Company;
});