1 回答

TA貢獻(xiàn)1846條經(jīng)驗(yàn) 獲得超7個(gè)贊
自己用的, 用到underscore的地方可以自己改一下
var COMMON = {};
COMMON.merge = function (target, source, source2) {
if (source && typeof source === 'object') {
if (!target || typeof target !== 'object') {
target = _.isArray(source) ? [] : {};
}
for (var key in source) {
if (source.hasOwnProperty(key)) {
if (source[key] && typeof source[key] === 'object') {
if (!target[key] || typeof target[key] !== 'object') {
target[key] = _.isArray(source[key]) ? [] : {};
}
COMMON.merge(target[key], source[key]);
} else {
target[key] = source[key];
}
}
}
}
return (source2 && typeof source2 === 'object') ? COMMON.merge(target, source2) : target;
};
COMMON.clone = function (obj) {
if (obj && typeof obj === 'object') {
if (_.isArray(obj)) {
return _.map(obj, function (item) {
return COMMON.clone(item);
});
} else {
return COMMON.merge({}, obj);
}
}
return obj;
};
添加回答
舉報(bào)