JavaScript catch parameter already defined

前端 未结 4 894
有刺的猬
有刺的猬 2021-02-01 05:57

I\'m trying to understand why I\'m getting the following error, not how to work around it.

Passing the following code to JSLint or JSHint yields the er

4条回答
  •  时光取名叫无心
    2021-02-01 06:48

    Wrote to Douglas Crockford, author of JSLint, about this issue.

    There turns out to be a very valid reason after all...

    Douglas writes:

    Catch variables are not scoped correctly, so I recommend that you use a different name in each one.

    If you look at this similar StackOverflow question you'll note that PleaseStand started to touch on it. Not all browsers, especially historic ones, handle scoping correctly or consistently.

    JSLint recognizes that your code may work in one browser, but not in another, leaving a very nasty and subtle bug to track down. The warning is real.

    By using a different name, which, yes, doesn't feel clean or concise at all, because it isn't, happens to be the universal way of not running into the problem.

    Thank you Douglas for clarifying! Mystery solved.

提交回复
热议问题