我已经编写了两种扩展和合并javascript对象的方法。这可能是个坏问题,但我不确定我做得对。你能看一下代码,告诉我是否需要改进一些东西,如果我做错了什么(代码工作乍一看)
merge: function (a, b) {
var copy = {};
CL.extend(copy, a, [HTMLElement, Array]);
CL.extend(copy, b, [HTMLElement, Array]);
return copy;
},
extend: function extend(a, b, excludeInstances) {
for (var prop in b)
if (b.hasOwnProperty(prop)) {
var isInstanceOfExcluded = false;
if (excludeInstances)
for (var i = 0; i < excludeInstances.length; i++)
if (b[prop] instanceof excludeInstances[i])
isInstanceOfExcluded = true;
if (typeof b[prop] === 'object' && !isInstanceOfExcluded) {
a[prop] = a[prop] !== undefined ? a[prop] : {};
extend(a[prop], b[prop], excludeInstances);
} else
a[prop] = b[prop];
}
}