extend
次のケースを処理する方法に関する実装 (またはパターン) はありますか? 私の知る限り、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;
}