You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The addition of feature detection in #327 is great. I think it would make a great addition to check for typeof.
if(fetch){fetch()}if(localStorage);// ReferenceErrorif(window.localStorage);// DOMException: Storage not allowed
This is generally unsafe as this would cause a ReferenceError if fetch did not exist. Instead, the safe and environment-agnostic way to check support for a built-in API is through typeof:
There is support for a different safe-ish mechanism already, namely window.fetch however I generally avoid this because 1) This can cause an exception if the property has a getter that denies access such as window.localStorage in some browser's private mode, and 2) is slightly slower, and 3) relies on there being a window global which doesn't work for isomorphic code that is agnostic of browser version and JS engine (e.g. also in Node.js, SpiderMonkey, especially versions prior to globalThis).
The typeof approach always works and seems to be common for this purpose.
I would recommend checking for either !== 'undefined' or === with anything other than 'undefined' (e.g. "object", "number", someVariable). I've not worked much with ESTree before, but I'd like to learn and would be interested in contributing a patch.
The text was updated successfully, but these errors were encountered:
I fully agree with that. Current implementation of feature detection is harmful and there even no option to disable it. For example following code is considered valid even if browser has no fetch support:
const url = tryToSomeUrl();
if (url) {
fetch(url);
}
The addition of feature detection in #327 is great. I think it would make a great addition to check for
typeof
.This is generally unsafe as this would cause a ReferenceError if
fetch
did not exist. Instead, the safe and environment-agnostic way to check support for a built-in API is throughtypeof
:There is support for a different safe-ish mechanism already, namely
window.fetch
however I generally avoid this because 1) This can cause an exception if the property has a getter that denies access such aswindow.localStorage
in some browser's private mode, and 2) is slightly slower, and 3) relies on there being awindow
global which doesn't work for isomorphic code that is agnostic of browser version and JS engine (e.g. also in Node.js, SpiderMonkey, especially versions prior toglobalThis
).The
typeof
approach always works and seems to be common for this purpose.I would recommend checking for either
!== 'undefined'
or===
with anything other than'undefined'
(e.g. "object", "number", someVariable). I've not worked much with ESTree before, but I'd like to learn and would be interested in contributing a patch.The text was updated successfully, but these errors were encountered: