ES2015/2016 way of 'typeof varName === 'undefined`?

前端 未结 2 1754
梦毁少年i
梦毁少年i 2021-01-31 13:41

I\'m wallowing in ES2015+ luxury with a few projects right now and am wondering whether I can get rid of the much hated crutch to check for undefined in the new won

2条回答
  •  佛祖请我去吃肉
    2021-01-31 14:34

    Just check for varName === undefined.

    In older browsers it was possible to assign an alternate value to the global undefined variable causing that test to fail, but in ES2015+ that's now impossible.

    Note that there's no way to distinguish explicitly passing undefined as a parameter from leaving the parameter out altogether other than by looking at arguments.length.

提交回复
热议问题