8

Can some one please explain me the difference between angular.merge and angular.extend..And what does deep copy means and when should it be used ?

2 Answers 2

12

Extend : to shallow copy the properties of the source objects from right to left, all the way to the destination object.

Example: extend person and job objects and vise versa.

       //------------------------------------Extend--------------------------

        $scope.extendPersonToJob = function () {
           var person = { 'Name': 'Monica', 'Age': '25', 'Skills': { 'name': 'travelling', 'place': 'Queenstown' } };
            var job = { 'Title': 'Programmer', 'Experience': '5',   'Skills': { 'name': 'Designing', 'experience': '2', 'certified': 'true' } };
            // extend from Person to Job

            $scope.personTojob = angular.extend(person, job);
             // output : { 'Name': 'Monica', 'Age': '25', 'Skills': { 'name': 'Designing', 'experience': '2', 'certified': 'true' } , 'Title': 'Programmer', 'Experience': '5'}             
        }

        $scope.extendJobToPerson = function () {
            var person = { 'Name': 'Monica', 'Age': '25', 'Skills': { 'name': 'travelling', 'place': 'Queenstown' } };
            var job = { 'Title': 'Programmer', 'Experience': '5', 'Skills': { 'name': 'Designing', 'experience': '2', 'certified': 'true' } };

            // extend from job to person
            $scope.jobToperson = angular.extend(job, person)
           // output : { 'Name': 'Monica', 'Age': '25', 'Skills': { 'name': 'travelling' , 'place': 'Queenstown' }  , 'Title': 'Programmer', 'Experience': '5'}             

        }

Merge is to deep (recursively) copy the properties of the source objects to the destination object.

Example: merge person and job objects and vise versa.

    //------------------------------------Merge------------------------------
        $scope.mergePersonToJob = function () {

            var person = { 'Name': 'Monica', 'Age': '25', 'Skills': { 'name': 'travelling', 'place': 'Queenstown' } };
            var job = { 'Title': 'Programmer', 'Experience': '5', 'Skills': { 'name': 'Designing', 'experience': '2', 'certified': 'true' } };
            // merge from Person to Job

            $scope.personTojob = angular.merge(person, job);

            // output :  { 'Name': 'Monica', 'Age': '25', 'Skills': { 'name': 'Designing', 'experience': '2', 'certified': 'true', 'place': 'Queenstown' }, 'Title': 'Programmer', 'Experience': '5' };
        }

        $scope.mergeJobToPerson = function () {
            var person = { 'Name': 'Monica', 'Age': '25', 'Skills': { 'name': 'travelling', 'place': 'Queenstown' } };
            var job = { 'Title': 'Programmer', 'Experience': '5', 'Skills': { 'name': 'Designing', 'experience': '2', 'certified': 'true' } };

            // merge from job to person
            $scope.jobToperson = angular.merge(job, person)

            // output : { 'Name': 'Monica', 'Age': '25', 'Skills': { 'name': 'travelling', 'experience': '2', 'certified': 'true', 'place': 'Queenstown' }, 'Title': 'Programmer', 'Experience': '5' };
        }

have a look this Example and compare for better understanding. please correct me if wrong.

PC : David Cai's Blog

Sign up to request clarification or add additional context in comments.

4 Comments

@Shijil Narayan i tried to explain deep (recursive) copy and shallow copy using Merge and extend with person and job objects example.
Thanks a lot Monica.The example you gave made things more clear.
@ShijilNarayan i am glad to help you. but i just came to know that my codepen example link was n't saved and showing default angular js page. i applologies for that. but i just have updated it have a look.
Yes ,but David Cai's blog link u shared helped me in understanding it more clearly.Will go through your codepen example too.Anyway Thank you
2

From angular docs.

Unlike extend(), merge() recursively descends into object properties of source objects, performing a deep copy.

3 Comments

Thanks Levi but still what does deep copy mean ?
@ShijilNarayan if you have nested objects, merge() will copy them as well, but using extend() doesn't.
Thanks Levi for the help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.