我有一个简单的问题。我正在与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 并监听调用事件 Function 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;
}
});
希望我的问题很清楚。如果我总结一下,我想在监听调用事件时调用函数 otherCall 一次,并在 callOther 函数内第二次调用函数 otherCall 。对于继承 EventEmitter,我想知道是否可以以不需要该行的方式修改代码,并且一切仍然有效。
最佳答案
像这样使用。它可能会起作用。
var self = this;
self.peerjs.on('call', function(call){
call.answer(window.localStream);
self.otherCall.call(self, call);
});
关于javascript - 如何在回调函数中调用继承的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31535700/