微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

以navigator.userAgent为例的JavaScript箭头功能

如何解决以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()方法

Source

解决方法

}

箭头功能结束


)

围绕箭头功能的分组运算符的结尾


(...)

使用参数调用函数


window.navigator.userAgent.toLowerCase()

争论


为什么在这里需要toLowerCase()方法?

因为它在进行不区分大小写的比较

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。