{"id":11719,"date":"2014-02-06T22:11:41","date_gmt":"2014-02-06T16:41:41","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=11719"},"modified":"2017-08-03T14:09:28","modified_gmt":"2017-08-03T08:39:28","slug":"angularjs-copy-vs-extend","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/angularjs-copy-vs-extend\/","title":{"rendered":"AngularJS : copy vs extend"},"content":{"rendered":"<p>While using\u00a0<a title=\"Angularjs development services\" href=\"http:\/\/www.tothenew.com\/front-end-angularjs-development\" target=\"_blank\">AngularJS<\/a>, you will usually come across this\u00a0situation when you want to copy one object to another object.<\/p>\n<p>In such case you will have only two solutions: angular.copy() or angular.extend().<\/p>\n<p>Lets see how these solutions work.<\/p>\n<p><strong>1. angular.copy(source, destination)<\/strong> : It creates a <strong>deep copy<\/strong> of source object or array and assign it to destination where &#8216;destination&#8217; is optional. By writing deep copy, I\u00a0mean that a new copy of the referred object is made.<\/p>\n<p>For example:<\/p>\n<p>[js]<br \/>\nvar mySource = {&#8216;name&#8217; : &#8216;sakshi&#8217;, &#8216;age&#8217; : &#8217;24&#8217;, &#8216;obj&#8217; :{&#8216;key&#8217;:&#8217;value&#8217;}}<br \/>\nvar myDest = {}<br \/>\nangular.copy(mySource, myDest);<br \/>\nconsole.log(myDest);<br \/>\n[\/js]<\/p>\n<p>Output: <code>{'name' : 'sakshi', 'age' : '24', 'obj' :{'key':'value'}}<\/code><\/p>\n<p>If you check <code>mySource.obj === myDest.obj<\/code>, this will give a false result because both points to different objects. This is called deep copying.<\/p>\n<p><strong>2. angular.extend(destination, src1, src2 &#8230;) <\/strong>: It creates a <strong>shallow copy<\/strong> of one or more sources provided and assign them to destination.<\/p>\n<p>For example:<\/p>\n<p>[js]<br \/>\nvar mySource1 = {&#8216;name&#8217; : &#8216;neha&#8217;, &#8216;age&#8217; : &#8217;26&#8217;, obj2 : {}}<br \/>\nvar mySource2 = {&#8216;course&#8217; : &#8216;MCA&#8217;}<br \/>\nvar myDest = {}<br \/>\nangular.extend(myDest, mySource1,mySource2)<br \/>\nconsole.log(myDest);<br \/>\n[\/js]<\/p>\n<p>Output: <code>{name: \"neha\", age: \"26\", course: \"MCA\", obj2: Object}<\/code><\/p>\n<p>Now if you check <code>mySource1.obj2 === myDest.obj2<\/code>, it will give a true result because both points to same reference of an object. This is called shallow copying.<\/p>\n<p><strong>NOTE<\/strong>: One thing to keep in mind is that angular.copy() is slower than angular.extend()<\/p>\n<p>You can now use both these solutions and find out which one you like to use in the future.<\/p>\n<p><span style=\"font-size: 20px; color: blue;\">Read Further on AngularJS Series<\/span><\/p>\n<ul>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-a-write-less-do-more-javascript-framework\/\">AngularJS :\u00a0A \u201cWrite Less Do More\u201d JavaScript Framework<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-updating-a-label-dynamically-with-user-input\/\">AngularJS :\u00a0Updating a Label Dynamically with User Input<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-adding-items-to-a-javascript-list-and-updating-corresponding-dom-dynamically\/\">AngularJS :\u00a0Adding Items to a JavaScript List and updating corresponding DOM Dynamically<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-text-suggestions\/\">AngularJS :\u00a0Text Suggestions<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-sorting-objects-on-various-attributes\/\">AngularJS :\u00a0Sorting objects on various attributes<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-fetching-data-from-the-server\/\">AngularJS :\u00a0Fetching data from the server<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-multiple-views-layout-template-and-routing\/\">AngularJS :\u00a0Multiple Views, Layout\u00a0Template\u00a0and Routing<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-implementing-routes-and-multiple-views\/\">AngularJS :\u00a0Implementing Routes And Multiple Views<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/building-intuitive-frontend-interfaces-with-angularjs\/\">Building Intuitive Frontend Interfaces with AngularJS<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>While using\u00a0AngularJS, you will usually come across this\u00a0situation when you want to copy one object to another object. In such case you will have only two solutions: angular.copy() or angular.extend(). Lets see how these solutions work. 1. angular.copy(source, destination) : It creates a deep copy of source object or array and assign it to destination [&hellip;]<\/p>\n","protected":false},"author":65,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":7},"categories":[1439,3429],"tags":[3955,955,4697,1304,1305],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/11719"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/65"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=11719"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/11719\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=11719"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=11719"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=11719"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}