Javascript 使用 underscore.js 进行空值检查
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10751043/
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
Using underscore.js for null checking
提问by John Cooper
var name = "someName";
if(name !=null) {
// do something
}
- I am right now using http://underscorejs.org/#isNull, how would i do
the same using
underscore.js
- Does it give any slight improvement in terms of performance for such functions.
- 我现在正在使用http://underscorejs.org/#isNull,我将如何使用
underscore.js
- 它是否在此类功能的性能方面略有改进。
回答by Peter Olson
In underscore, you can use
在下划线中,您可以使用
if(!_.isNull(name)) {}
and in plain Javascript, you should use
并且在纯 Javascript 中,您应该使用
if(name !== null) {}
You should avoid the loose inequality operator !=
because it does type coercion and undefined != null
will return false
.
您应该避免使用松散的不等式运算符,!=
因为它确实输入了强制类型并且undefined != null
会返回false
。
Using plain Javascript is slightly faster because it doesn't have to invoke a function, but it will be imperceptible and it should hardly be a consideration.
使用普通 Javascript 稍微快一点,因为它不必调用函数,但它不会被察觉,几乎不应该考虑。
I don't have a strong preference either way as far as readability goes, but it seems a little excessive and verbose to call a library function for such a simple check.
就可读性而言,我对任何一种方式都没有强烈的偏好,但为这样一个简单的检查调用库函数似乎有点过分和冗长。
回答by Chuck Norris
In underscore.js you must write this to achieve that functionality.
在 underscore.js 中,您必须编写它来实现该功能。
var name = "someName";
if(!(_.isNull(name ))) {
// do something
}
In underscore.js function isNull
is written like this
在 underscore.js 函数isNull
是这样写的
_.isNull = function(obj) {
return obj === null;
};
So the difference is using ==
in your code and ===
in underscore.js.For more details about that difference you can look in this question.
所以区别在于==
在您的代码和===
underscore.js 中使用。有关该区别的更多详细信息,您可以查看此问题。
Which equals operator (== vs ===) should be used in JavaScript comparisons?
在 JavaScript 比较中应该使用哪个等于运算符 (== vs ===)?
P.S. I will suggest to write your own condition instead of using any library in such simple place.
PS 我会建议写你自己的条件,而不是在这样简单的地方使用任何库。
var name = "someName";
if(name !== null)) {
// do something
}
回答by Niet the Dark Absol
Well your original code is flawed because if name
is an empty string, false
, the number 0
or any other falsy value then it will be considered null
as far as your test is concerned.
好吧,您的原始代码有缺陷,因为如果name
是空字符串、false
数字0
或任何其他虚假值,那么就null
您的测试而言,它将被考虑在内。
As a general rule, calling ANY function is an overhead that should be avoided if possible. In this case, calling a function just to test if a value is null, when you could very easily just write if( name === null)
, is just stupid, IMO...
作为一般规则,调用 ANY 函数是一种应该尽可能避免的开销。在这种情况下,调用一个函数只是为了测试一个值是否为空,当你可以很容易地编写时if( name === null)
,只是愚蠢的,IMO...
回答by Ashutosh Soni
if ((!(_.isUndefined(data)) || _.isEmpty(data))) {
//Valid Data
}else {//Not Valid}