简体   繁体   中英

How to call a inherited function inside a callback function

I have a quick question. I am making video chat with peerjs and I get the error that function is undefined. Here is the code:

Main constructor is Voip which is called in other file like

var voip = new Voip(); 

This is the function:

function Voip(options) {
var self = this;

options = options ||  {};

var allOptions = _.extend({
secure: true,
debug: 3
}, options);

this.peerjs = new Peer(allOptions);

First problem is here. How could I call otherCall inside callback function with listening to call event Function otherCall is at the bottom. Now it's written with this.otherCall but that doesn't work. I would just like to go to this function whenever I receive call event and answer the call.

this.peerjs.on('call', function(call){
call.answer(window.localStream);
this.otherCall(call);
  });
}

And then the Voip is extended with inheriting EventEmitter. Could I get rid of this line completely and still maintain same functionality? I don't use EventEmitter at all but was used in code I helped with.

Voip.prototype = _.extend(EventEmitter.prototype, {

And same here, self.otherCall doesn't work. What is the solution?

callOther: function(receiverId) {
var self = this;
var call = self.peerjs.call(receiverId, window.localStream);
self.otherCall(call);
},

otherCall: function(call) {

if (window.existingCall) {
    window.existingCall.close();
  }

call.on('stream', function(stream){
$('iframe').putthecodein....(stream)
  });
  window.existingCall = call;
}
});

Hope I've been clear in my question. If I summarize I want to call function otherCall once when listening to call event and second time inside callOther function. And for inheriting EventEmitter I wonder if I can modify code in such way that I don't need that line and everything still works.

Use like this. It might work.

var self = this;

self.peerjs.on('call', function(call){
    call.answer(window.localStream);
    self.otherCall.call(self, call);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM