How to detect operator support?
Using try/catch alone is impossible. You can try/catch expressions but not new syntax. For example this code wouldn't even parse:
const support_dollar_operator = () => {
try {
1 $ 2;
return true;
} catch (e) {
return false;
}
}
support_dollar_operator(); // your browser exploded before that
However it is possible with eval:
const support_dollar_operator = () => {
try {
eval(`1 $ 2`);
return true;
} catch (e) {
return false;
}
}
support_dollar_operator();
//=> false
Is it possible to polyfill an operator?
Yes and no.
Yes you can use a new operator with tools such as Babel but behind the scene it will rewrite your code if that operator isn't supported by your target audience. It would look like this:
// your code
bar ?? 'baz'
// transpiled
bar == undefined ? 'baz' : bar
How do you fix your issue?
You haven't posted an error message so we can't assume that your issue is caused by ?? not being supported.
This would fail even if ?? is supported:
var foo;
foo.bar ?? 'baz';
//=> ERR: Uncaught TypeError: Cannot read property 'bar' of undefined
The ?? operator isn't meant to provide a fallback if something along the path is undefined. This is the job of the ?. "optional chaining" operator:
var foo;
foo?.bar ?? 'baz';
//=> 'baz'
In your code an error could be caused if either A or B is undefined:
(this.currentElements[0].labels[0].textContent ?? 'This field')
// ^ ^
// A B
The good news is that the ?. operator is supported in Safari on iOS