javascript 测试 .length 和 .length > 0
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11992073/
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
javascript testing .length and .length > 0
提问by frenchie
I have some text in an object's property. I'm testing to see if the object's property has text in it to display; if it doesn't then I display "-" instead of just a blank. It doesn't seem like there's a difference between:
我在对象的属性中有一些文本。我正在测试对象的属性中是否有要显示的文本;如果没有,那么我会显示“-”而不是空白。似乎没有区别:
if (MyObject.SomeText && MyObject.SomeText.length) { ... }
if (MyObject.SomeText && MyObject.SomeText.length > 0) { ... }
Are there any edge cases where one syntax would be preferable to the other?
是否存在一种语法优于另一种语法的边缘情况?
回答by Matt Ball
Are there any edge cases where one syntax would be preferable to the other?
是否存在一种语法优于另一种语法的边缘情况?
Only edge cases where MyObject.SomeText
or MyObject.SomeText.length
is not what you expect it to be – for instance:
仅在边缘情况下MyObject.SomeText
或MyObject.SomeText.length
不是您期望的情况 - 例如:
MyObject = {
SomeText = {
length: -42
// or length: true
}
};
回答by Eric Yin
they give same result. Btw, if its "text", then if (MyObject.SomeText)
is enough
他们给出相同的结果。顺便说一句,如果它的“文本”,那么if (MyObject.SomeText)
就足够了
回答by Cranio
No, they are equivalent, if the length equals to 0 then it is valued as false
.
不,它们是等价的,如果长度等于 0 则它的值为false
。
(This is possible because JS is not strongly typed, in strongly typed languages length would not be castable as boolean).
(这是可能的,因为 JS 不是强类型的,在强类型语言中,长度不能转换为布尔值)。
回答by jbabey
In javascript, a number is only considered "falsey" when it is 0. Any other value is "truthy". Therefore, the statements number != 0
(comparison, not identity) and !number
are exactly equivalent.
在 javascript 中,一个数字只有在它为 0 时才被认为是“falsey”。任何其他值都是“truthy”。因此,语句number != 0
(比较,不是同一)和!number
完全等价。
The only way your two statements would differ is if length
was something other than a positive number.
您的两个语句不同的唯一方式是 iflength
不是正数。
回答by Oriol
It's the same:
一样的:
Boolean(MyObject.SomeText.length)
- returns
true
ifMyObject.SomeText.length
!= 0 - returns
false
ifMyObject.SomeText.length
== 0
true
如果MyObject.SomeText.length
!= 0 则返回false
如果MyObject.SomeText.length
== 0 则返回
and
和
Boolean(MyObject.SomeText.length>0)
- returns
true
ifMyObject.SomeText.length
> 0 - returns
false
ifMyObject.SomeText.length
<= 0
true
如果MyObject.SomeText.length
> 0 则返回false
如果MyObject.SomeText.length
<= 0 则返回
But MyObject.SomeText.length
can only be 0
or a positive integrer. So
但MyObject.SomeText.length
只能是0
或正积分器。所以
- If
MyObject.SomeText.length
== 0,Boolean(MyObject.SomeText.length)
returnsfalse
becauseMyObject.SomeText.length
== 0Boolean(MyObject.SomeText.length>0)
returnsfalse
becauseMyObject.SomeText.length
<=0
- If
MyObject.SomeText.length
> 0,Boolean(MyObject.SomeText.length)
returnstrue
becauseMyObject.SomeText.length
!= 0Boolean(MyObject.SomeText.length>0)
returnstrue
becauseMyObject.SomeText.length
> 0
- 如果
MyObject.SomeText.length
==0,Boolean(MyObject.SomeText.length)
返回false
因为MyObject.SomeText.length
== 0Boolean(MyObject.SomeText.length>0)
返回false
因为MyObject.SomeText.length
<=0
- 如果
MyObject.SomeText.length
> 0,Boolean(MyObject.SomeText.length)
返回true
因为MyObject.SomeText.length
!= 0Boolean(MyObject.SomeText.length>0)
返回true
因为MyObject.SomeText.length
> 0