Parameter passed without explicitly stating javascript Chrome extension

本秂侑毒 提交于 2019-12-13 07:13:08

问题


So this example chrome extension calls a selector for media streams and then the gotStream(stream) clearly is looking for a parameter of a mediaObject. However, when the gotStream function is passed in as the second parameter to navigator.webkitGetUserMedia no parameter is passed in. It merely remains states the function name.

Here is the code

function gotStream(stream) {
  console.log("Received local stream");
  var video = document.querySelector("video");
  video.src = URL.createObjectURL(stream);
  localstream = stream;
  stream.onended = function() { console.log("Ended"); };
}

function getUserMediaError() {
  console.log("getUserMedia() failed.");
}

function onAccessApproved(id) {
  if (!id) {
    console.log("Access rejected.");
    return;
  }
  navigator.webkitGetUserMedia({
      audio:false,
      video: { mandatory: { chromeMediaSource: "desktop",
                            chromeMediaSourceId: id } }
  }, gotStream, getUserMediaError);
}

I've seen these kinds of calls before and I just don't get how they work, please help.


回答1:


Is there an error of some sorts, or you just don't understand how that works?

navigator.webkitGetUserMedia expects 3 arguments. One for configuration data, one for a function to call on success (a success callback) and one to call on error.

Note that it expects a function. This code passes a reference to the function by name; it will be called with the appropriate parameter (from within webkitGetUserMedia).

Consider this code, it works in the same manner:

function hello(subject) {
  alert("Hello, " + subject + "!");
}

function passWorldTo(callback) {
  callback("world");  
}

passWorldTo(hello);


来源:https://stackoverflow.com/questions/26168732/parameter-passed-without-explicitly-stating-javascript-chrome-extension

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!