【发布时间】:2013-11-06 00:29:18
【问题描述】:
在这里,我正在尝试在 javascript 中构造对象。 但我收到运行时错误:
TypeError: Cannot set property 'functWriteToLogFile' of undefined
我的 javascript 对象如下:
function SetAstAppLog(logFolderPath,fileNamePrefix,fileSize,logStreamObject) {
.
.
.
.
this.functWriteToLogFile = function (fileNamePrefix, message) {
console.log("functWriteToLogFile " + message);
var currLogStreamObject = initLogPath(fileNamePrefix);
console.log("********************");
console.log(fileNamePrefix);
console.log(filePath);
currLogStreamObject.write(message + '\n');
this.emit('written');
};
initLogPath(fileNamePrefix);// Set log path on creation of new object.
this.emit('objCreated');
}
我想在其他功能中访问functWriteToLogFile:
SetAstAppLog.prototype.funcLogErrors = function (fileNamePrefix,errLevel,err,req) {
//make new json object here then call functWriteToLogFile
this.functWriteToLogFile(fileNamePrefix, JSON.stringify(logErrorObj));
};
我在这里找不到我的错误。
谁能帮我解决这个问题?
编辑:
我通过以下方式调用这个函数:
var SetAstAppLog = require('astAppLog')();
var fileSize = 1024;
var objCommLogger = new SetAstAppLog(logFolderPath,logCommFilePrefix,fileSize,logCommMsg);
【问题讨论】:
-
你能在小提琴中展示你所有的代码吗?这很奇怪。
-
这与您的其他问题相同。如果您在那边收到的答案不起作用,不要接受它。也就是说,您对该问题的原始答案是错误的,但现在已得到纠正。您的问题是您使用的答案不正确。
-
显示实际使用“funcLogErrors”函数的代码。
标签: javascript node.js prototype