Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在回调函数中调用继承函数_Javascript_Jquery_Inheritance_Callback_Peerjs - Fatal编程技术网

Javascript 如何在回调函数中调用继承函数

Javascript 如何在回调函数中调用继承函数,javascript,jquery,inheritance,callback,peerjs,Javascript,Jquery,Inheritance,Callback,Peerjs,我有一个简短的问题。我正在与peerjs进行视频聊天,我得到的错误是函数未定义。代码如下: 主构造函数是Voip,它在其他文件中调用,如 var voip = new Voip(); 这就是功能: function Voip(options) { var self = this; options = options ||  {}; var allOptions = _.extend({ secure: true, debug: 3 }, options); this.peerjs =

我有一个简短的问题。我正在与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位于底部。现在它是用这个.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);
});

在您的
call
事件中,您的
otherCall
行抛出错误吗?
callOther
函数也有同样的问题。您是否尝试过记录
调用
事件中的
这个
是什么,以及
self
函数中的
callOther
是什么。这可能是范围问题。@TahirAhmed Yes
otherCall
不能在该行的两种情况下都调用。另外,
self
inside
callOther
是Voip对象,
inside call事件未定义。您的
otherCall
函数是在
Voip
对象中定义的,还是仅仅是一个全局函数?它在这个
Voip.prototype=\uu.extend(EventEmitter.prototype,{
这就是为什么我问我是否可以摆脱这个扩展和继承,以及如何摆脱。因为在这种情况下,我可以调用这个函数,就像它在Voip中一样。你是否可以创建一个JSFIDLE呢?它确实可以工作,调用函数,但我在这一行上遇到了问题
调用('stream',函数(stream))
内部
otherCall
函数。它表示无法读取未定义的
上的属性
。问题在于没有在正确的时间接收流。该函数是在流打开之前调用的。