为什么JSLint告诉我使用"=== undefined"而不是"typeof ... ==='undefined'"? [英] Why does JSLint tell me to use "=== undefined" instead of "typeof ... === 'undefined'"?

查看:98
本文介绍了为什么JSLint告诉我使用"=== undefined"而不是"typeof ... ==='undefined'"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了以下代码:

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

但是JSLint在说:

But JSLint is saying:

警告3 JS Lint:意外的'typeof'.使用'==='直接与undefined进行比较.

Warning 3 JS Lint: Unexpected 'typeof'. Use '===' to compare directly with undefined.

我应该如何更改代码?

推荐答案

请注意,这通常是否是最佳实践尚有待商,,但如果您想使其与JSLint一起使用,则可以这样做

Note that whether this is best practice in general is debatable, but if you want to make it work with JSLint, you could do this

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

这篇关于为什么JSLint告诉我使用"=== undefined"而不是"typeof ... ==='undefined'"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆