Browse files

Edited object#merge. Copy input for instead of manipulating them dire…

…ctly. And removed the try...catch
  • Loading branch information...
1 parent b1d69c5 commit 9ac833f0b62dac3878192017420d679d77dce6f8 @larzconwell larzconwell committed Sep 27, 2012
Showing with 15 additions and 15 deletions.
  1. +15 −15 lib/object.js
View
30 lib/object.js
@@ -34,25 +34,25 @@ var object = new (function () {
@param {Object} otherObject Object to read from
*/
this.merge = function (object, otherObject) {
- object = object || {};
- otherObject = otherObject || {};
- var i, key, value;
+ var obj = object || {}
+ , otherObj = otherObject || {}
+ , key, value;
- for (i in otherObject) {
- key = i, value = otherObject[key];
+ for (key in otherObj) {
+ value = otherObj[key];
- try {
- // If value is an object
- if (typeof value === 'object' && !(value instanceof Array)) {
- // Update value of object to the one from otherObject
- object[key] = merge(object[key], value);
- } else object[key] = value;
- } catch (err) {
- // Object isn't set so set it
- object[key] = value;
+ // Check if a value is an Object, if so recursively add it's key/values
+ if (typeof value === 'object' && !(value instanceof Array)) {
+ // Update value of object to the one from otherObj
+ obj[key] = merge(obj[key], value);
+ }
+ // Value is anything other than an Object, so just add it
+ else {
+ obj[key] = value;
}
}
- return object;
+
+ return obj;
};
/**

0 comments on commit 9ac833f

Please sign in to comment.