在这里,我正在尝试在 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);