V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
nfroot
V2EX  ›  问与答

JS 兼容写法直接 try catch 就好了?不用判断啥浏览器了?

  •  
  •   nfroot · Mar 22, 2017 · 2829 views
    This topic created in 3326 days ago, the information mentioned may be changed or developed.
    举个例子, attachEvent 我首先是判断是否 IE ,不是 IE 就用 addEventListener 。

    然后发现新版 IE 变成了 addEventListener 了……

    于是直接 try catch 。涉及到框架的操作,新版和旧版又不同,于是又 try catch ……

    于是我发现判断 IE 的那段代码完全无用了……因为直接 try catch 就好了。总不能还每个浏览器去判断对应版本吧。
    4 replies    2017-04-01 22:38:59 +08:00
    nfroot
        1
    nfroot  
    OP
       Mar 22, 2017
    大牛别笑我,这个肯定不适合所有场景……
    aleen42
        3
    aleen42  
       Apr 1, 2017
    try-catch 并非是最优化解,因为你如果直接 try-catch ,就会产生诸如 scope-chain 扩大化的问题
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2753 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 13:17 · PVG 21:17 · LAX 06:17 · JFK 09:17
    ♥ Do have faith in what you're doing.