1/* 2* Count the number of object fields 3* 4* @return number of the fields 5* @access public 6*/ 7Object.prototype.length = function () { 8 var i = 0; 9 for (var k in this) { 10 if ('function'!=typeof this.hasOwnProperty || !this.hasOwnProperty(k)) continue; 11 i++; 12 } 13 return i; 14} 15/* 16* Clone object 17* 18* @param optional object to clone 19* @return cloned object 20* @access public 21*/ 22Object.prototype.clone = function (obj) { 23 if (typeof(obj) != "object") return obj; 24 try { var newObject = new obj.constructor(); } catch(e) {return null;} 25 for (var objectItem in obj) { 26 if (!obj.hasOwnProperty(objectItem)) continue; 27 newObject[objectItem] = obj.clone(obj[objectItem]); 28 } 29 return newObject; 30} 31 32/* 33* Merge objects v0.2 34* 35* + fixed merge of arrays 36* 37* @param object to merge 38* @param boolean overwrite same keys or no 39* @return void 40* @access public 41*/ 42Object.prototype.merge = function (obj, overwrite) { 43 /* 44 * overwrite by default 45 */ 46 try { var n = new obj.constructor(); } catch(e) {return null;} 47 try { 48 if (isUndefined(overwrite)) overwrite = true; 49 for (var i in obj) { 50 if (!obj.hasOwnProperty(i)) continue; 51 if (isUndefined(this[i]) || (overwrite && typeof this[i] != typeof obj)) 52 if (obj[i] instanceof Array) this[i] = []; 53 else if ('object' == typeof obj[i]) this[i] = {}; 54 if (obj[i] instanceof Array) this[i] = this[i].concat(obj[i]); 55 else if ('object' == typeof obj[i]) this[i].merge(obj[i], overwrite); 56 else if (isUndefined(this[i]) || overwrite) this[i] = obj[i]; 57 } 58 } catch(e) {return this} 59} 60 61 62/* 63* Checks if property is derived from prototype, applies method if it is not exists 64* 65* @param string property name 66* @return bool true if prototyped 67* @access public 68*/ 69if ('undefined' == typeof Object.hasOwnProperty) { 70 Object.prototype.hasOwnProperty = function (prop) { 71 return !('undefined' == typeof this[prop] || this.constructor && this.constructor.prototype[prop] && this[prop] === this.constructor.prototype[prop]); 72 } 73} 74