文件名:orderManager.js
const notificationManager = require("./notificationManager");
var orderManager = (function() {
function orderManager() {
console.log("orderManager");
};
orderManager.bootstrap = function() {
console.log("orderManager.bootstrap");
return new orderManager();
};
orderManager.prototype.orderContext = {
"CONTEXT_1": "context_1",
"CONTEXT_2": "context_2",
"CONTEXT_3": "context_3"
};
orderManager.prototype.notify = function() {
var dataObj = {};
notificationManager.sendToClient(dataObj);
};
return orderManager;
}());
module.exports = orderManager.bootstrap();
文件名:notificationManager.js
const orderManager = require("./orderManager");
var notificationManager = (function() {
function notificationManager() {
console.log("notificationManager");
};
notificationManager.bootstrap = function() {
console.log("notificationManager.bootstrap");
return new notificationManager();
};
notificationManager.prototype.sendToClient = function(dataObj) {
console.log("notificationManager.prototype.sendToClient");
var _this = this;
switch (_this.request.body.orderContext) {
case orderManager.orderContext.CONTEXT_1:
notifyClient(_this, dataObj);
break;
}
};
return notificationManager;
}());
module.exports = notificationManager.bootstrap();
当我尝试从 orderManager.js 文件调用 notificationManager.sendToClient()函数时,会出现错误 TypeError:无法读取属性&#39 ; CONTEXT_1'未定义的。
答案 0 :(得分:1)
您不能将orderManager.js和notificationManager.js紧密联系在一起。
根据您的代码段,您尝试执行orderManager.notify().
您的Javascript编译流程将如下
const notificationManager = require("./notificationManager");
const orderManager = require("./orderManager");
const orderManager = {} // Not an instance of orderManager
的实例这里我修改了你的片段并删除了orderManager.js中的notificationManager.js依赖关系
<强> orderManager.js 强>
var orderManager = (function() {
function orderManager() {
console.log("orderManager");
};
orderManager.bootstrap = function() {
console.log("orderManager.bootstrap");
return new orderManager();
};
orderManager.prototype.orderContext = {
"CONTEXT_1": "context_1",
"CONTEXT_2": "context_2",
"CONTEXT_3": "context_3"
};
return orderManager;
}());
module.exports = orderManager.bootstrap();
文件名:notificationManager.js
const orderManager = require("./orderManager");
var notificationManager = (function() {
function notificationManager() {
console.log("notificationManager");
};
notificationManager.bootstrap = function() {
console.log("notificationManager.bootstrap");
return new notificationManager();
};
notificationManager.prototype.sendToClient = function(dataObj) {
console.log("notificationManager.prototype.sendToClient");
var _this = this;
switch (_this.request.body.orderContext) {
case orderManager.orderContext.CONTEXT_1:
notifyClient(_this, dataObj);
break;
}
};
return notificationManager;
}());
module.exports = notificationManager.bootstrap();
文件名:index.js
const a = require('./notificationManager');
a.sendToClient();