如何在回调函数中调用继承函数
How to call a inherited function inside a callback function
我有一个简短的问题。我正在与 peerjs 进行视频聊天,但收到函数未定义的错误。这是代码:
主要构造函数是 Voip,它在其他文件中被调用,例如
var voip = new Voip();
这是函数:
function Voip(options) {
var self = this;
options = options || {};
var allOptions = _.extend({
secure: true,
debug: 3
}, options);
this.peerjs = new Peer(allOptions);
第一个问题来了。我怎么能通过监听调用事件调用回调函数中的 otherCall 函数 otherCall 在底部。现在它是用 this.otherCall 写的,但这不起作用。每当我收到呼叫事件并接听电话时,我只想转到此功能。
this.peerjs.on('call', function(call){
call.answer(window.localStream);
this.otherCall(call);
});
}
然后继承EventEmitter扩展Voip。我可以完全摆脱这条线并仍然保持相同的功能吗?我根本不使用 EventEmitter,但在我帮助编写的代码中使用了它。
Voip.prototype = _.extend(EventEmitter.prototype, {
同样这里,self.otherCall 不起作用。解决办法是什么?
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;
}
});
希望我的问题已经清楚了。如果我总结一下,我想在监听 call 事件时调用函数 otherCall 一次,然后在 callOther 函数中调用第二次。为了继承 EventEmitter,我想知道我是否可以以不需要该行并且一切仍然有效的方式修改代码。
这样使用。可能有用。
var self = this;
self.peerjs.on('call', function(call){
call.answer(window.localStream);
self.otherCall.call(self, call);
});
我有一个简短的问题。我正在与 peerjs 进行视频聊天,但收到函数未定义的错误。这是代码:
主要构造函数是 Voip,它在其他文件中被调用,例如
var voip = new Voip();
这是函数:
function Voip(options) {
var self = this;
options = options || {};
var allOptions = _.extend({
secure: true,
debug: 3
}, options);
this.peerjs = new Peer(allOptions);
第一个问题来了。我怎么能通过监听调用事件调用回调函数中的 otherCall 函数 otherCall 在底部。现在它是用 this.otherCall 写的,但这不起作用。每当我收到呼叫事件并接听电话时,我只想转到此功能。
this.peerjs.on('call', function(call){
call.answer(window.localStream);
this.otherCall(call);
});
}
然后继承EventEmitter扩展Voip。我可以完全摆脱这条线并仍然保持相同的功能吗?我根本不使用 EventEmitter,但在我帮助编写的代码中使用了它。
Voip.prototype = _.extend(EventEmitter.prototype, {
同样这里,self.otherCall 不起作用。解决办法是什么?
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;
}
});
希望我的问题已经清楚了。如果我总结一下,我想在监听 call 事件时调用函数 otherCall 一次,然后在 callOther 函数中调用第二次。为了继承 EventEmitter,我想知道我是否可以以不需要该行并且一切仍然有效的方式修改代码。
这样使用。可能有用。
var self = this;
self.peerjs.on('call', function(call){
call.answer(window.localStream);
self.otherCall.call(self, call);
});