如何识别 requireJS 的功能? -- Javascript
how to recognize to function with requireJS ? -- Javascript
我多次尝试使用 requireJS 来获取特定的依赖项。我知道循环依赖有不同的方法,我在 Whosebug 中阅读了 Q/A 但我没有。我在下面的代码和我收到以下错误。如何修复此错误?我在这里试过this method。提前致谢。
错误:
Uncaught TypeError: require(...).fullNameAll is not a function
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;
});
我用 "exports" 解决了我的问题。
解决方案:
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;
});
我多次尝试使用 requireJS 来获取特定的依赖项。我知道循环依赖有不同的方法,我在 Whosebug 中阅读了 Q/A 但我没有。我在下面的代码和我收到以下错误。如何修复此错误?我在这里试过this method。提前致谢。
错误:
Uncaught TypeError: require(...).fullNameAll is not a function
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;
});
我用 "exports" 解决了我的问题。 解决方案:
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;
});