我有這樣的JavaScript類:的Javascript,嵌套事件處理程序,這
var Server = (function() {
var spawn = require('child_process').spawn;
function Server(serverDefinition) {
this.definition = serverDefinition;
this.status = false;
}
Server.prototype.start = function() {
this.process = spawn('java', ['-jar', this.definition.jarfile]);
this.status = true;
this.process.on('exit', function(code, signal){
this.status = false;
console.log('Server stopped: code=' + code + ', signal=' + signal);
});
this.process.stdout.on('data', function(data){ console.log('stdout: ' + data);});
this.process.stderr.on('data', function(data){ console.log('stderr: ' + data);});
};
return Server;
})();
我的問題是this
內的this.process.on('exit', ...)
referes到process
,不Server
,我想它。
處理這種情況的最佳模式是什麼? A _self = this
?在那種情況下,應該在哪裏插入該行,並且我應該停止提及this
,而只使用_self
來代替?
similiar http://stackoverflow.com/questions/13167630/why-is-the-this-variable-not-the-expected-value – Musa