Why does JSLint tell me to use “=== undefined” instead of “typeof … === 'undefined'”?

后端 未结 3 1127
日久生厌
日久生厌 2021-01-11 09:46

I coded the following:

showTitles = (typeof showTitles !== \'undefined\') ? showTitles : \'Y\';
showSelectGroup = (typeof showSelectGroup !== \'undefined\')          


        
3条回答
  •  没有蜡笔的小新
    2021-01-11 09:57

    Probably by using

    showTitles = (showTitles === undefined) ? 'Y' : showTitles;
    showSelectGroup = (showSelectGroup === undefined) ? 'Y' : showSelectGroup;
    

    jslint has no issues with that (assuming showTitles and showSelectGroup are declared with var)

    However, I'd write it as

    var showTitles = showTitles || 'Y';
    var showSelectGroup = showSelectGroup || 'Y';
    

提交回复
热议问题