Refactor object.js.

This commit is contained in:
Tobie Langel 2008-09-30 01:04:25 +02:00
parent 48f5ff2072
commit 0b646c01c6
1 changed files with 81 additions and 63 deletions

View File

@ -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
});
})();