JavaScript中的getUserMedia()在各种浏览器中正常化。非法调用 [英] getUserMedia() in JavaScript normalizes across browsers. Illegal Invocation

查看:3656
本文介绍了JavaScript中的getUserMedia()在各种浏览器中正常化。非法调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试执行以下操作时:

  var getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia; 
//现在我尝试用一​​些参数调用它:
getUserMedia(...)//不工作!

在Chrome中引发错误非法调用。



但如果我这样做:

  navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia; 
//现在用导航器调用它
navigator.getUserMedia(..)// Works

我尝试了一下搜索,并且我看到它是一个上下文问题。但我仍然不明白这是什么意思。在第一个例子中,getUserMedia变量最终得到了一个不是undefiend的函数的引用(例如,在chrome的情况下,它是webkitGetUserMedia),那么为什么我不能使用这个变量来调用它?



(这实际上是一个通用的JavaScript问题,并不特定于 WebRTC < a>。)

显然,它需要上下文是 navigator 对象,无论出于何种原因。我注意到与 console.log 相同的事情 - 它需要控制台上下文。



执行 navigator.getUserMedia 时,上下文会自动设置为 navigator ,与其他任何时候一样,您在对象上调用方法。但是,如果您仅执行 getUserMedia ,则上下文(默认情况下)是全局的,因此会引发非法调用错误。 b
$ b

如果您仍想将其保存为变量,则可以在运行时使用正确的上下文调用它:

  var getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia; 
//现在我尝试用一​​些参数调用它:
getUserMedia.call(navigator,...)

您也可以使用 bind 来保存上下文中的变量,这样您就不必每次都调用它: p>

  var getUserMedia =(navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia).bind(navigator); 
//现在我试着用一些参数调用它:
getUserMedia(...)


When I try to do the following:

var getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia;
// now I try to invoke it with some parameters:
getUserMedia(...) // not working!

It throws an error "Illegal Invocation" in Chrome.

But if I do:

navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia;
// now invoke it with the navigator
navigator.getUserMedia(..) // Works

I've tried searching a bit, and I read it's a context issue. But I still couldn't understand what's the meaning of that. In the first example, the getUserMedia variable ends up getting a reference to the function which is not undefiend (i.e, in case of chrome, it's webkitGetUserMedia), so why cannot I invoke it using this variable?

(This is actually a general JavaScript question, not specific to WebRTC.)

解决方案

Apparently, it needs the context to be the navigator object, for whatever reason. I've noticed the same thing with console.log - it needs the console context.

When you do navigator.getUserMedia, the context is automatically set to navigator, as with any other time you invoke a method on an object. If you just do getUserMedia though, the context (by default) is global, and so it throws an Illegal Invocation error.

If you still want to save it as a variable, you can call it with the correct context at runtime:

var getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia;
// now I try to invoke it with some parameters:  
getUserMedia.call(navigator, ...)

You could also use bind to save the context with the variable, so that you don't have to call it each time:

var getUserMedia = (navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia).bind(navigator);
// now I try to invoke it with some parameters:  
getUserMedia(...)

这篇关于JavaScript中的getUserMedia()在各种浏览器中正常化。非法调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆