Object on which a function is called gets lost subsequently in the callbacks. How could I preserve/maintain the object on which the function was called.
var javaci = new Javaci();
javaci.run(actionObject.source);
javaci.once('f2run',this.output);
javaci.once('jvmo',this.output);
The above snippet (which is altogether in a different class)calls a run
method of class Javaci
and then listens for 2 events, f2run
and jvmo
.
But the callback function this.output
is never called.
The following function successfully calls the subsequent compileAndRun
function.
Javaci.prototype.run = function(code) {
if(code) {
var classDetails = this.classToInject(code);
if(Object.keys(classDetails).length > 0) {
code = `${classDetails['classToInject']}${code}`;
var writer = new Writer();
writer.write(code,`${classDetails['className']}.java`);
writer.once('written',this.compileAndRun);
writer.once('failed',this.failedToWrite);
}
}
}
When the control enters the following function, this
refers to the object of Writer
class. How could I avoid this? Perhaps, this is the reason, the event listener function is never fired.
Javaci.prototype.compileAndRun = function(fileName) {
if(fileName) {
const child2 = execFile('javac', [fileName], (error, stdout, stderr) => {
if (error) {
this.emit('f2run',error);
}else {
var classFile = fileName.replace('.java','').trim();
execFile('java', [classFile], (error, stdout, stderr) => {
if (error) {
this.emit('f2run',error);
}else {
this.emit('jvmo',stdout);
}
});
}
});
}
}