class MyExampleClass {
constructor(port, service) {
this.port = port;
this.service = service;
this.app = require('express')();
this.http = require('http').createServer(this.app);
}
onListening() {
// Current result: 'listening on port NaN for undefined'
// Desired result: 'listening on port 3000 for test' (I want to CHANGE this behavior)
console.log('listening on port %d for %s', this.port, this.service);
// Current result: 'class: Server'
// Desired result: 'class: Server' (I want to KEEP this behavior)
console.log('class: ' + this.constructor.name);
}
start() {
this.http.listen(this.port, this.onListening);
}
}
const example = new MyExampleClass(3000, 'test');
example.start();
As stated in the comments of the onListening
method, I want to access both the context of the instance of MyExampleClass
and the callback instance of Server
created by createServer
.
const self=this
somewhere before the context switch and using self instead of this when referencing the other contextself
in as another parameter toonListening
or is there a better way so as not to alter the method signature?