admin管理员组

文章数量:1394159

I have some objects with properties. I wanted to test to see if they had characters in them so I initially wrote this:

if (MyObject.Prop1.length > 0) {....}

However, sometimes the object may not have a certain property so I was getting the error "cannot get length".

I changed it by writing this:

if (MyObject.Prop1 && MyObject.Prop1.length > 0) {....}

I'm using the chrome inspector and when I run the code, I don't get the error anymore. Is this going to work in every browser?

Thanks.

I have some objects with properties. I wanted to test to see if they had characters in them so I initially wrote this:

if (MyObject.Prop1.length > 0) {....}

However, sometimes the object may not have a certain property so I was getting the error "cannot get length".

I changed it by writing this:

if (MyObject.Prop1 && MyObject.Prop1.length > 0) {....}

I'm using the chrome inspector and when I run the code, I don't get the error anymore. Is this going to work in every browser?

Thanks.

Share Improve this question asked Dec 7, 2011 at 22:50 frenchiefrenchie 52.1k117 gold badges320 silver badges528 bronze badges 5
  • 3 Are you sure Prop1 will always be a string? Because now, if it contains the Number 0, your condition will be false. And numbers don't have a length property also. – kapa Commented Dec 7, 2011 at 22:52
  • @baz: well if it contains 0 then the length won't be larger than zero and thus the call probably shouldn't be made anyway – Martin Jespersen Commented Dec 7, 2011 at 22:57
  • @Martin All depends on the logic of the application :). – kapa Commented Dec 7, 2011 at 22:59
  • What happens when we have this: MyObject.Prop1 = 4; ? I only have strings ... for now, but's still WIP. – frenchie Commented Dec 7, 2011 at 23:05
  • @baz: nevermind i am tired and need sleep, sorry for my useless and very wrong ment. – Martin Jespersen Commented Dec 7, 2011 at 23:07
Add a ment  | 

3 Answers 3

Reset to default 5

As an alternative:

if ('Prop1' in MyObject && MyObject.Prop1.length > 0) { ... )

Or, to be even more careful:

if (MyObject.hasOwnProperty('Prop1') && MyObject.Prop1.length > 0) { ... }

Of course that might be the wrong thing to do, depending on the nature of "MyObject".

Yes it will work quite fine, although you can save yourself the > 0 and just do

if (MyObject.Prop1 && MyObject.Prop1.length) {....}

since anything other than zero will evaluate to true.

Since undefined, 0, and "" are all "falsy" in JavaScript, this is equivalent:

if(MyObject.Prop1) {
  // ...
}

Rick Waldron's "Idiomatic JavaScript" is a good reference for simplifying conditional statements without sacrificing correctness. You can test its use yourself:

function testProp(val) {
  if(val) {
    return val.length;
  }

  return "nope!";
}

var myObj = { stringProp   : "foo",
              emptyStrProp : "",
              // undefinedProp is not defined
              zeroProp     : 0,
              twelveProp   : 12,
              otherObjProp : document.createElement('div'),
              arrayProp    : [ 'a', 'b' ]
            };

console.log( testProp( myObj.stringProp    ) ); // => 3
console.log( testProp( myObj.emptyStrProp  ) ); // => "nope!"
console.log( testProp( myObj.undefinedProp ) ); // => "nope!"

// of course if you're expecting values other than strings and undefined
// you'll have to account for them

console.log( testProp( myObj.zeroProp      ) ); // => "nope!"
console.log( testProp( myObj.twelveProp    ) ); // => undefined
console.log( testProp( myObj.otherObjProp  ) ); // => undefined
console.log( testProp( myObj.arrayProp     ) ); // => 2

本文标签: javascripttesting object property lengthStack Overflow