简体   繁体   English

javascript测试.length和.length> 0

[英]javascript testing .length and .length > 0

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? 在某些情况下,一种语法比另一种更可取吗?

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.SomeTextMyObject.SomeText.length不是您期望的情况下,例如:

MyObject = {
    SomeText = {
        length: -42
        // or length: true
    }
};

they give same result. 他们给出相同的结果。 Btw, if its "text", then if (MyObject.SomeText) is enough 顺便说一句,如果它的“文本”,那么if (MyObject.SomeText)就足够了

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不是强类型的,在强类型的语言中,长度不能转换为布尔值)。

In javascript, a number is only considered "falsey" when it is 0. Any other value is "truthy". 在javascript中,数字仅在为0时才被视为“假”。任何其他值均为“ truthy”。 Therefore, the statements number != 0 (comparison, not identity) and !number are exactly equivalent. 因此,语句number != 0 (比较,而不是标识)和!number完全相等。

The only way your two statements would differ is if length was something other than a positive number. 如果length不是正数,则这两个语句唯一的区别是。

It's the same: 一样的:

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

and

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

But MyObject.SomeText.length can only be 0 or a positive integrer. 但是MyObject.SomeText.length只能为0或正整数。 So 所以

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM