是否有关于如何处理以下extend
情况的任何实现(或模式)?AFAIK在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;
}