Detect if AudioContext() constructor accepts AudioContextOptions argument at runtime
Reported by
to...@helloeko.com,
May 2 2017
|
|||
Issue descriptionUserAgent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36 Steps to reproduce the problem: I'd love to use the latencyHint feature: https://www.chromestatus.com/feature/5678699475107840 Unfortunately, the following code will throw a TypeError on Firefox: new AudioContext({latencyHint: 'playback'}); I'm not aware of a decent way to detect at runtime whether AudioContext() accepts an options argument (other than browser detection). One solution might be for Chrome to report an arity of 1 for the AudioContext() constructor. So I could write the following code: var audioCtx; if (AudioContext.length) { audioCtx = new AudioContext({latencyHint: 'playback'}); } else { audioCtx = new AudioContext(); } What is the expected behavior? The value of AudioContext.length should be 1. What went wrong? The value of AudioContext.length is 0. Did this work before? No Does this work in other browsers? N/A Chrome version: 58.0.3029.81 Channel: stable OS Version: 10.0 Flash Version:
,
May 2 2017
Thanks for your quick reply! You're right, not really a Chrome bug, but it would be nice if the constructor had the correct arity. Also filed an issue with Mozilla: https://bugzilla.mozilla.org/show_bug.cgi?id=1361475 And yes, try/catch would work.
,
May 2 2017
Isn't the correct arity 0? The AudioOptions dictionary is optional.
,
May 2 2017
I guess you could argue that, but I think 1 would be more idiomatic.
If I were to implement this constructor in JS it would probably look something like this:
function AudioContext(options) {
options = options || {};
...
}
The AudioContext.length value of this function is 1.
However, looking at other examples of native constructors, it seems that optional arguments are not counted.
The only exception I could find is the Error() constructor.
All arguments are optional but Error.length is 1:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error
In any case - not a major issue, so feel free to close if you think this is irrelevant.
,
May 2 2017
Yeah, I poked around a bit too and Math.max.length is 2, even though it supports any number of arguments.
,
May 2 2017
Ok, thanks for looking into it :)
,
May 4 2017
,
May 4 2017
Wontfix: working as intended, not a problem in Chrome. |
|||
►
Sign in to add a comment |
|||
Comment 1 by rtoy@chromium.org
, May 2 2017This isn't really a problem with Chrome is it? And shouldn't the following work for detecting support? var c; try { c = new AudioContext({latencyHint: "playback"}); } catch (e) { console.log("AudioContext options not supported"); }