问题
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