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 Number0
, your condition will befalse
. 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
3 Answers
Reset to default 5As 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
版权声明:本文标题:javascript : testing object property length - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744683225a2619541.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论