本文介绍了JavaScript中的getUserMedia()在各种浏览器中正常化。非法调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 当我尝试执行以下操作时: 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),那么为什么我不能使用这个变量来调用它? 解决方案 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()在各种浏览器中正常化。非法调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 09-02 04:52