修正了几个BUG,已经在实际项目中使用,没有问题。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
Array.prototype.indexOf || (Array.prototype.indexOf = function(value) { for (var i in this) { if (this[i] == value) { return i; } } return -1; }); Array.prototype.remove = function(b) { var a = this.indexOf(b); if (a >= 0) { this.splice(a, 1); return true; } return false; }; window.jlUtil = { getJSON: function(obj, skip_arr) { if (typeof obj == "number" || typeof obj == "boolean") { return obj; } else if (typeof obj == "string") { return "'" + obj.replace("'", "\\'") + "'"; } else if (!obj) { return "''"; } var json = []; skip_arr = skip_arr ? skip_arr : []; for (var key in obj) { //skip function and the keys in skip arr if (typeof obj[key] == "function" || skip_arr.indexOf(key) != -1) { continue; } //in Array loop if (!isNaN(key) || key.constructor != String) { json.push(jlUtil.getJSON(obj[key], skip_arr)); continue; } //else recurrency json.push("'" + key + "':" + jlUtil.getJSON(obj[key], skip_arr)); } if (Object.prototype.toString.apply(obj) === '[object Array]') { return "[" + json.join(",") + "]"; } else { return "{" + json.join(",") + "}"; } }, extend: function(obj) { if (arguments.length == 1) { this.extend(this, obj); return this; } else if (arguments.length >= 2) { var dst = arguments[0]; //extend the rest args to args[0] for (var i = 1; i < arguments.length; ++i) { for (var item in arguments[i]) { var tmp = arguments[i][item]; if (typeof arguments[i][item] == "object") { //make a copy while is a object, needs jQuery here var tmp = $.extend({}, arguments[i][item]); } //if is object try recurrency if (dst[item] && typeof dst[item] == "object") { try { this.extend(dst[item], tmp); } catch (e) { dst[item] = tmp; } } else { dst[item] = tmp; } } } return dst; } } }; |