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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 14:59:28  来源:igfitidea点击:

javascript testing .length and .length > 0

javascript

提问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.SomeTextor MyObject.SomeText.lengthis not what you expect it to be – for instance:

仅在边缘情况下MyObject.SomeTextMyObject.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 !numberare exactly equivalent.

在 javascript 中,一个数字只有在它为 0 时才被认为是“falsey”。任何其他值都是“truthy”。因此,语句number != 0(比较,不是同一)和!number完全等价。

The only way your two statements would differ is if lengthwas something other than a positive number.

您的两个语句不同的唯一方式是 iflength不是正数。

回答by Oriol

It's the same:

一样的:

Boolean(MyObject.SomeText.length)
  • returns trueif MyObject.SomeText.length!= 0
  • returns falseif MyObject.SomeText.length== 0
  • true如果MyObject.SomeText.length!= 0 则返回
  • false如果MyObject.SomeText.length== 0 则返回

and

Boolean(MyObject.SomeText.length>0)
  • returns trueif MyObject.SomeText.length> 0
  • returns falseif MyObject.SomeText.length<= 0
  • true如果MyObject.SomeText.length> 0 则返回
  • false如果MyObject.SomeText.length<= 0 则返回

But MyObject.SomeText.lengthcan only be 0or a positive integrer. So

MyObject.SomeText.length只能是0或正积分器。所以

  • If MyObject.SomeText.length== 0,
    • Boolean(MyObject.SomeText.length)returns falsebecause MyObject.SomeText.length== 0
    • Boolean(MyObject.SomeText.length>0)returns falsebecause MyObject.SomeText.length<=0
  • If MyObject.SomeText.length> 0,
    • Boolean(MyObject.SomeText.length)returns truebecause MyObject.SomeText.length!= 0
    • Boolean(MyObject.SomeText.length>0)returns truebecause MyObject.SomeText.length> 0
  • 如果MyObject.SomeText.length==0,
    • Boolean(MyObject.SomeText.length)返回false因为MyObject.SomeText.length== 0
    • Boolean(MyObject.SomeText.length>0)返回false因为MyObject.SomeText.length<=0
  • 如果MyObject.SomeText.length> 0,
    • Boolean(MyObject.SomeText.length)返回true因为MyObject.SomeText.length!= 0
    • Boolean(MyObject.SomeText.length>0)返回true因为MyObject.SomeText.length> 0