javascript - Difference between angular.merge and angular.extend? -
can 1 please explain me difference between angular.merge , angular.extend..and deep copy means , when should used ?
extend : shallow copy properties of source objects right left, way destination object.
example: extend person , job objects , 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 person 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 job person $scope.jobtoperson = angular.extend(job, person) //output : {{ 'name': 'monica', 'age': '25', 'skills': { 'name': 'travelling' , 'place': 'queenstown' } , 'title': 'programmer', 'experience': '5'} }
merge deep (recursively) copy properties of source objects destination object.
example: merge person , job objects , 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 person 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 job 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 example , compare better understanding. please correct me if wrong.
pc : david cai's blog
Comments
Post a Comment