admin 管理员组文章数量: 1086019
I'm confused with JSLint.
My code originally checked if div:jqmData("me")
was undefined like so:
if ( typeof el.jqmData("me") == "undefined"
? el.not(':jqmData(panel="main")').length > 0
: el.not(':jqmData(me="first")').length > 0 ){
}
JSLint plains that I should replace checking with typeof
with ===
, so I did like this:
if ( el.jqmData("me") === "undefined"
? el.not(':jqmData(panel="main")').length > 0
: el.not(':jqmData(me="first")').length > 0 ){
}
JSLint doesn't plain anymore, but my nested if statement is broken, because I'm now always ending up with the 2nd if el.not(':jqmData(me="first")').length
even when I should not.
Question:
Why does JSLint remend ===
over typeof == undefined
? How es this breaks my logic?
Thanks for some enlightment...
I'm confused with JSLint.
My code originally checked if div:jqmData("me")
was undefined like so:
if ( typeof el.jqmData("me") == "undefined"
? el.not(':jqmData(panel="main")').length > 0
: el.not(':jqmData(me="first")').length > 0 ){
}
JSLint plains that I should replace checking with typeof
with ===
, so I did like this:
if ( el.jqmData("me") === "undefined"
? el.not(':jqmData(panel="main")').length > 0
: el.not(':jqmData(me="first")').length > 0 ){
}
JSLint doesn't plain anymore, but my nested if statement is broken, because I'm now always ending up with the 2nd if el.not(':jqmData(me="first")').length
even when I should not.
Question:
Why does JSLint remend ===
over typeof == undefined
? How es this breaks my logic?
Thanks for some enlightment...
Share Improve this question asked Nov 2, 2012 at 22:36 frequentfrequent 28.6k61 gold badges187 silver badges336 bronze badges 5-
10
x === undefined
, notx === 'undefined'
– zerkms Commented Nov 2, 2012 at 22:38 - @zerksm: please make it an answer! – frequent Commented Nov 2, 2012 at 22:39
-
4
Depending on what you're checking, there's an advantage in using
typeof someVar === 'undefined'
oversomeVar === undefined
, namely that the former won't break ifsomeVar
isn't, in fact, defined. For example, ifsomeVar
hasn't been declared, you'll get aReferenceError
using the latter, while the former works without a hitch. See: stackoverflow./questions/4725603/… ( cc @zerkms ) – NullUserException Commented Jan 9, 2013 at 21:43 - @NullUserException: yep, makes sense for checking variables (as an opposite to properties) – zerkms Commented Jan 9, 2013 at 22:05
- Possible duplicate of Why does jslint tell me to use ===? – Matthew Simoneau Commented Dec 1, 2015 at 3:25
2 Answers
Reset to default 6You've broken the parison logic. It's assumed you use
typeof el.jqmData("me") === "undefined"
or
el.jqmData("me") === undefined
Personally I'd go with the latter.
And personally I think that this particular JSLint check in this particular case makes not much sense.
What zerkms wrote is correct. An explanation can help though, from https://github./jamesallardice/jslint-error-explanations/issues/10#issuement-18273885:
A modernization of style has occurred with undefined
parison. ES5 guarantees that undefined
is undefined
. In strict mode, in parison with the old and new styles, the typeof "undefined"
check is longer to type and is no longer necessary now that undefined can be pared directly.
See the JSLint discussion: https://plus.google./101248256976407044060/posts/Q5oFnnxG9oL
Crockford basically says that typeof "undefined"
check is longer and slower and is unnecessary.
本文标签: javascriptwhy does JSLint recommend xquotundefinedquot vs typeof xquotundefinedquotStack Overflow
版权声明:本文标题:javascript - why does JSLint recommend x === "undefined" vs. typeof x == "undefined"? - Stac 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/p/1744076932a2529524.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论