angularjs - extend recursive

前端 未结 6 1530
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-10 10:59

I would like to extend some properties recursive (aka. deep copy). much like jQuery does. I\'m not including jquery only b/c of one thing.

jQuery.extend( tru         


        
6条回答
  •  醉梦人生
    2020-12-10 11:20

    Building on Ryan's code, you can shorten the object check and you should also NOT extend functions so you don't override object pointers.

    var extendDeep = function extendDeep(dst) {
        angular.forEach(arguments, function(obj) {
            if (obj !== dst) {
                angular.forEach(obj, function(value, key) {
                    if (dst[key] && angular.isObject(dst[key])) {
                        extendDeep(dst[key], value);
                    } else if(!angular.isFunction(dst[key])) {
                        dst[key] = value;
                    }
                });
            }
        });
        return dst;
    };
    

提交回复
热议问题