From ec5069ed3adc77852486209f5079e7bbe3b8495f Mon Sep 17 00:00:00 2001 From: Tobie Langel Date: Tue, 30 Sep 2008 01:04:25 +0200 Subject: [PATCH] Refactor object.js. --- src/lang/object.js | 144 +++++++++++++++++++++++++-------------------- 1 file changed, 81 insertions(+), 63 deletions(-) diff --git a/src/lang/object.js b/src/lang/object.js index 0473220..124596c 100644 --- a/src/lang/object.js +++ b/src/lang/object.js @@ -1,22 +1,22 @@ -Object.extend = function(destination, source) { - for (var property in source) - destination[property] = source[property]; - return destination; -}; +(function() { + function extend(destination, source) { + for (var property in source) + destination[property] = source[property]; + return destination; + } -Object.extend(Object, { - inspect: function(object) { + function inspect(object) { try { - if (Object.isUndefined(object)) return 'undefined'; + if (isUndefined(object)) return 'undefined'; if (object === null) return 'null'; return object.inspect ? object.inspect() : String(object); } catch (e) { if (e instanceof RangeError) return '...'; throw e; } - }, - - toJSON: function(object) { + } + + function toJSON(object) { var type = typeof object; switch (type) { case 'undefined': @@ -24,73 +24,91 @@ Object.extend(Object, { case 'unknown': return; case 'boolean': return object.toString(); } - + if (object === null) return 'null'; if (object.toJSON) return object.toJSON(); - if (Object.isElement(object)) return; - + if (isElement(object)) return; + var results = []; for (var property in object) { - var value = Object.toJSON(object[property]); - if (!Object.isUndefined(value)) + var value = toJSON(object[property]); + if (!isUndefined(value)) results.push(property.toJSON() + ': ' + value); } - + return '{' + results.join(', ') + '}'; - }, - - toQueryString: function(object) { + } + + function toQueryString(object) { return $H(object).toQueryString(); - }, - - toHTML: function(object) { + } + + function toHTML(object) { return object && object.toHTML ? object.toHTML() : String.interpret(object); - }, - - keys: function(object) { + } + + function keys(object) { var keys = []; for (var property in object) keys.push(property); return keys; - }, - - values: function(object) { + } + + function values(object) { var values = []; for (var property in object) values.push(object[property]); return values; - }, - - clone: function(object) { - return Object.extend({ }, object); - }, - - isElement: function(object) { - return !!(object && object.nodeType == 1); - }, - - isArray: function(object) { - return object != null && typeof object == "object" && - 'splice' in object && 'join' in object; - }, - - isHash: function(object) { - return object instanceof Hash; - }, - - isFunction: function(object) { - return typeof object == "function"; - }, - - isString: function(object) { - return typeof object == "string"; - }, - - isNumber: function(object) { - return typeof object == "number"; - }, - - isUndefined: function(object) { - return typeof object == "undefined"; } -}); + + function clone(object) { + return extend({ }, object); + } + + function isElement(object) { + return !!(object && object.nodeType == 1); + } + + function isArray(object) { + return object != null && typeof object === "object" && + 'splice' in object && 'join' in object; + } + + function isHash(object) { + return object instanceof Hash; + } + + function isFunction(object) { + return typeof object === "function"; + } + + function isString(object) { + return typeof object === "string"; + } + + function isNumber(object) { + return typeof object === "number"; + } + + function isUndefined(object) { + return typeof object === "undefined"; + } + + extend(Object, { + extend: extend, + inspect: inspect, + toJSON: toJSON, + toQueryString: toQueryString, + toHTML: toHTML, + keys: keys, + values: values, + clone: clone, + isElement: isElement, + isArray: isArray, + isHash: isHash, + isFunction: isFunction, + isString: isString, + isNumber: isNumber, + isUndefined: isUndefined + }); +})();