So this is an uncomfortable question, but I'm learning NodeJS and I have a question. In Java, when I call a method from an object, the this instance remains the same (as in this example).
private Test inst; public Test() { inst = this; this.myFunction(); } private void myFunction() { System.out.println(inst == this); }
This returns true (theoretically, this code is from the top of my head). However, in NodeJS, when I try to do something like this, it fails.
var MyObject = function () { this.other = new OtherObject(); this.other.on("error", this.onError); console.log(this);
My question is: why is this so, and if it does not work correctly on my part, how can I correctly refer to other variables in the MyObject instance from the onError method?
source share