我正在开发一个React.js web应用程序,其中包含使用Intl (国际化对象)的数据表。以下错误仅在尝试在旧的Safari浏览器上加载应用程序时出现。
这是当前用于检查Intl是否可用的方法,如果不可用,则返回到localCompare()
function getCollatorComparator() {
if (Intl) return new Intl.Collator(void 0, { numeric: !0, sensitivity: "base" }).compare;
return function(e, t) {
return (e + "").localeCompare(t)
}
}上面的代码在传统的Safari (iOS 9)上不起作用。
如何检查Intl是否可用?
发布于 2019-03-06 19:42:48
当你执行if(Intl)时,你会得到一个错误,因为没有定义Intl,类似于这样:
if(foo) { // foo not defined -> thus crash
console.log("foo");
} else {
console.log("bar"); // not executed
}
但是,如果您使用typeof,您可以检查以前是否没有声明变量:
if(typeof foo !== "undefined") { // no crash (foo is undefined)
console.log("foo");
} else {
console.log("bar"); // bar is outputted
}
因此,不用使用(如果没有定义Intl就会抛出一个错误):
if(Intl) // code...您可以使用:
if(typeof Intl !== "undefined") // code...https://stackoverflow.com/questions/55022017
复制相似问题