如何解决以navigator.userAgent为例的JavaScript箭头功能
const browser = ((agent) => {
switch (true) {
case agent.indexOf("edge") > -1: return "edge";
case agent.indexOf("edg") > -1: return "chromium based edge (dev or canary)";
case agent.indexOf("opr") > -1 && !!window.opr: return "opera";
case agent.indexOf("chrome") > -1 && !!window.chrome: return "chrome";
case agent.indexOf("trident") > -1: return "ie";
case agent.indexOf("firefox") > -1: return "firefox";
case agent.indexOf("safari") > -1: return "safari";
default: return "other";
}
}) (window.navigator.userAgent.toLowerCase());
有一个简单的示例,说明如何使用navigator.userAgent
对象的Window
属性检测浏览器。有人可以解释这段代码的最新行实际上是做什么的,为什么这里需要toLowerCase()
方法?
解决方法
}
箭头功能结束
)
围绕箭头功能的分组运算符的结尾
(...)
使用参数调用函数
window.navigator.userAgent.toLowerCase()
争论
为什么在这里需要toLowerCase()方法?
因为它在进行不区分大小写的比较
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。