0

A JavaScript object is used to store error messages. Works great if there are errors, but if no errors (object is blank) the code fails. So, I added the jQuery $.isEmptyObject() test -- and now it fails if there is content.

Why does this code fail when trying to read the size of the object?

jsFiddle

HTML:

<div>If visible, code not work</div>

jQuery:

var tmp;
var objErr = new Object();

objErr['test'] = 'bob';

if ($.isEmptyObject(objErr) ){
    $('div').hide();
}else{    
    tmp = Object.size(objErr); //<=== fails here
    $('div').hide(); //if not work, js is broken
}
if (tmp > 1){
    $('div').hide();
    alert(tmp);
}else{
    $('div').hide();
}
halfer
  • 19,824
  • 17
  • 99
  • 186
crashwap
  • 2,846
  • 3
  • 28
  • 62
  • 2
    what is `Object.size`? Press F12 and read your console. – Kevin B Mar 19 '15 at 21:06
  • try `Object.keys(objErr).length` instead of `Object.size(objErr)` a faster way: `for(var tmp in objErr) break;` – dandavis Mar 19 '15 at 21:10
  • And there you go... the dangers of re-using poorly documented code. I thought Object.size() was a native function, but it is from here: http://stackoverflow.com/questions/5223/length-of-a-javascript-object-that-is-associative-array. Thanks gents. – crashwap Mar 19 '15 at 21:12
  • Please add comment as answer for credit – crashwap Mar 19 '15 at 21:12

0 Answers0