有沒有關於如何處理以下extend
個案的任何實現(或模式)? AFAIK無法在Angular或Underscore中直接執行此操作,對嗎?嵌套對象擴展和跳過未定義的屬性(在Angular或Underscore中)
否則這裏是我的實現,但我想知道是否有任何已經完成或無論如何,知道您對我的代碼的反饋,謝謝!
http://jsbin.com/welcome/52916/edit
/**
Extends the target object with the properties in the source object, with the following special handling:
- it doesn't extend undefined properties, i.e.
target: { a: 10 }
source: { a: undefined }
result: { a: 10 }
- it does nested extends rather than overwriting sub-objects, i.e.
target: { b: { i: 'Hi' } }
source: { b: { j: 'Bye' } }
result: { b: { i: 'Hi', j: 'Bye' } }
*/
function extend(target, source) {
_.each(_.keys(source), function(k) {
if (angular.isDefined(source[k])) {
if (angular.isObject(source[k])) {
extend(definedOr(target[k], {}), source[k]);
}
else {
target[k] = source[k];
}
}
});
return target;
}