I've got a class, something like this:
class Server {
constructor() {
this.server = http.createServer(function (req, res) {
this.doSomething()
});
}
doSomething() {
console.log("Working")
}
}
I want to call my doSomething() function from inside the constructor, how can I do this? I've tried doing this.doSomething() and doSomething(), both say they are not a function. Also, say in the constructor I did console.log(this.someValue), it logs undefined. How can I access the classes own properties/methods? Is it even possible? Thanks.
this.doSomething()will call it. If you get an error, post the code that produces that errorthisis referring to the callback function on the HTTP server. Is there a way I can almost "go up 1 level" withthiskeyword?http.createServer((req, res) => { ... }