How to get method object in JS using this?

obj1.prototype.f1= function(){
 requester = zmq.socket('req');
requester.connect(config.handshakeSocket);
 requester.on('message', function(data){
this.f2();
}
}
obj1.prototype.f2= function(){
}


when referring to this received requester, and how to obj1?
September 19th 19 at 13:34
1 answer
September 19th 19 at 13:36
Solution
obj1.prototype.f1= function(){
 var _that = this;
 requester = zmq.socket('req');
requester.connect(config.handshakeSocket);
 requester.on('message', function(data){
 // _that == obj1
_that.f2();
}
}
obj1.prototype.f2= function(){
}


This is called snapping, and it says a lot of things. Here, for example: habrahabr.ru/post/38642
Yes, thanks, I saw the last line. Corrected - Isac_Mra commented on September 19th 19 at 13:42
only here it is necessary to write so
requester.on('message', function(data){
_that.f2(); // instead of this.f2();
} - brock commented on September 19th 19 at 13:39

Find more questions by tags JavaScript