检查 JavaScript 对象是否为 null subtelty
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18421995/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Checking JavaScript object for null subtelty
提问by Peter Kellner
I believe I have found cases where I need to check for both undefined and null for a javascript object as follows:
我相信我已经找到了需要检查 javascript 对象的 undefined 和 null 的情况,如下所示:
if (x !== undefined && x != null && x.length > 0) {...}
However, in a recent upgrade of JetBrains tools, it tells me that this is sufficient
但是,在最近的 JetBrains 工具升级中,它告诉我这已经足够了
if (x != undefined && x.length > 0) {...}
My question is, I'm just wanting to insure that a string "x" has a length of non-zero and is not undefined or null (with the least amount tests).
我的问题是,我只是想确保字符串“x”的长度不为零,并且不是未定义的或空的(使用最少的测试)。
Thoughts?
想法?
回答by gdoron is supporting Monica
in javascript
在 JavaScript 中
undefined == null // true
undefined === null // false
so checking with ==
for undefined
makes the ==
check for null
redundant.
所以检查==
forundefined
会使==
检查变得null
多余。
回答by Dennis
Try
尝试
if (x && x.length)
as undefined
, null
and 0
are all falsy values.
as undefined
,null
和0
都是假值。
EDIT:
as you seem to know that x
should be a string
, you can also just use if (x)
as an empty string is also falsy.
编辑:正如你似乎知道x
应该是 a string
,你也可以只使用if (x)
一个空字符串也是假的。
回答by Alaeddine
You can use _.isNull
from Underscorea JavaScript library provides a whole mess of useful functional programming helpers.
你可以_.isNull
从Underscore使用一个 JavaScript 库,它提供了一大堆有用的函数式编程助手。
_.isNull(object)
_.isNull(对象)
Returns true if the value of object is null.
如果 object 的值为 null,则返回 true。
_.isNull(null);
=> true
_.isNull(undefined);
=> false
回答by James Drinkard
This is what I use and it's the most concise. It covers: undefined, null, NaN, 0, "" (empty string), or false. We can therefore say that "object" is truthy.
这是我使用的,它是最简洁的。它涵盖:未定义、空值、NaN、0、""(空字符串)或 false。因此,我们可以说“对象”是真实的。
if(object){
doSomething();
}
回答by Wex
Checking if foo === undefined
will trigger the error foo is not defined. See variable === undefined vs. typeof variable === "undefined"
检查是否foo === undefined
会触发错误foo is not defined。请参阅变量 === 未定义与 typeof 变量 === “未定义”
The existential operatorin CoffeeScript compiles to
CoffeeScript 中的存在运算符编译为
typeof face !== "undefined" && face !== null
Edit:
编辑:
Matt's commentis better if you only want to check for strings:
如果您只想检查字符串,Matt 的评论会更好:
typeof x === 'string' && x.length > 0
回答by jukempff
To check against null
AND undefined
AND "empty string" you can just write
要检查null
AND undefined
AND “空字符串”,您可以只写
if(!x) {
// will be false for undefined, null and length=0
}
BUT you need to be sure that your variable is defined! Otherwise this will cause an error.
但是您需要确保您的变量已定义!否则这将导致错误。
If you are checking for a value in an object
(e.g. the window
object), you can always use that. E.g. for checking for localStorage
support:
如果您正在检查object
(例如window
对象)中的值,您始终可以使用它。例如检查localStorage
支持:
var supports = {
localStorage: !!window.localStorage
}
回答by iJade
Try this
试试这个
if (!x) {
// is emtpy
}