{"id":11719,"date":"2014-02-06T22:11:41","date_gmt":"2014-02-06T16:41:41","guid":{"rendered":"https:\/\/2thenew.online\/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:\/\/2thenew.online\/blog\/angularjs-copy-vs-extend\/","title":{"rendered":"AngularJS : copy vs extend"},"content":{"rendered":"<p>While using\u00a0<a title=\"Angularjs development services\" href=\"https:\/\/2thenew.online\/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=\"https:\/\/2thenew.online\/blog\/angularjs-a-write-less-do-more-javascript-framework\/\">AngularJS :\u00a0A \u201cWrite Less Do More\u201d JavaScript Framework<\/a><\/li>\n<li><a href=\"https:\/\/2thenew.online\/blog\/angularjs-updating-a-label-dynamically-with-user-input\/\">AngularJS :\u00a0Updating a Label Dynamically with User Input<\/a><\/li>\n<li><a href=\"https:\/\/2thenew.online\/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=\"https:\/\/2thenew.online\/blog\/angularjs-text-suggestions\/\">AngularJS :\u00a0Text Suggestions<\/a><\/li>\n<li><a href=\"https:\/\/2thenew.online\/blog\/angularjs-sorting-objects-on-various-attributes\/\">AngularJS :\u00a0Sorting objects on various attributes<\/a><\/li>\n<li><a href=\"https:\/\/2thenew.online\/blog\/angularjs-fetching-data-from-the-server\/\">AngularJS :\u00a0Fetching data from the server<\/a><\/li>\n<li><a href=\"https:\/\/2thenew.online\/blog\/angularjs-multiple-views-layout-template-and-routing\/\">AngularJS :\u00a0Multiple Views, Layout\u00a0Template\u00a0and Routing<\/a><\/li>\n<li><a href=\"https:\/\/2thenew.online\/blog\/angularjs-implementing-routes-and-multiple-views\/\">AngularJS :\u00a0Implementing Routes And Multiple Views<\/a><\/li>\n<li><a href=\"https:\/\/2thenew.online\/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":23,"footnotes":""},"categories":[1439,3429],"tags":[3955,955,4697,1304,1305],"class_list":["post-11719","post","type-post","status-publish","format-standard","hentry","category-angular","category-front-end-development","tag-angular-development","tag-angularjs","tag-angularjs-development-company","tag-copy","tag-extend"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 5.0.0.1 - aioseo.com -->\n\t<meta name=\"description\" content=\"Learn different utility functions of AngularJS such as copy and extend\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"Sakshi Tyagi\"\/>\n\t<link rel=\"canonical\" href=\"https:\/\/2thenew.online\/blog\/angularjs-copy-vs-extend\/\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 5.0.0.1\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"TO THE NEW BLOG\" \/>\n\t\t<meta property=\"og:type\" content=\"blog\" \/>\n\t\t<meta property=\"og:title\" content=\"AngularJS : copy vs extend | TO THE NEW Blog\" \/>\n\t\t<meta property=\"og:description\" content=\"Learn different utility functions of AngularJS such as copy and extend\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/2thenew.online\/blog\/angularjs-copy-vs-extend\/\" \/>\n\t\t<meta property=\"og:image\" content=\"https:\/\/2thenew.online\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<meta property=\"og:image:secure_url\" content=\"https:\/\/2thenew.online\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary\" \/>\n\t\t<meta name=\"twitter:site\" content=\"@tothenew\" \/>\n\t\t<meta name=\"twitter:title\" content=\"AngularJS : copy vs extend | TO THE NEW Blog\" \/>\n\t\t<meta name=\"twitter:description\" content=\"Learn different utility functions of AngularJS such as copy and extend\" \/>\n\t\t<meta name=\"twitter:image\" content=\"https:\/\/2thenew.online\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<script type=\"application\/ld+json\" class=\"aioseo-schema\">\n\t\t\t{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/angularjs-copy-vs-extend\\\/#article\",\"name\":\"AngularJS : copy vs extend | TO THE NEW Blog\",\"headline\":\"AngularJS : copy vs extend\",\"author\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/sakshi\\\/#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\"},\"datePublished\":\"2014-02-06T22:11:41+05:30\",\"dateModified\":\"2017-08-03T14:09:28+05:30\",\"inLanguage\":\"en-US\",\"commentCount\":11,\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/angularjs-copy-vs-extend\\\/#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/angularjs-copy-vs-extend\\\/#webpage\"},\"articleSection\":\"AngularJS, Front End Development, angular development, AngularJS, AngularJS Development Company, copy, extend\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/angularjs-copy-vs-extend\\\/#breadcrumblist\",\"itemListElement\":[{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog#listItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.tothenew.com\\\/blog\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/angular\\\/#listItem\",\"name\":\"AngularJS\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/angular\\\/#listItem\",\"position\":2,\"name\":\"AngularJS\",\"item\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/angular\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/angularjs-copy-vs-extend\\\/#listItem\",\"name\":\"AngularJS : copy vs extend\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/angularjs-copy-vs-extend\\\/#listItem\",\"position\":3,\"name\":\"AngularJS : copy vs extend\",\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/angular\\\/#listItem\",\"name\":\"AngularJS\"}}]},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\",\"name\":\"TO THE NEW Blog\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/sakshi\\\/#author\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/sakshi\\\/\",\"name\":\"Sakshi Tyagi\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/angularjs-copy-vs-extend\\\/#authorImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/a42c6deb088e18815dfcfed629c77b032d17bf973881421880b9c5e4ba713739?s=96&d=mm&r=g\",\"width\":96,\"height\":96,\"caption\":\"Sakshi Tyagi\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/angularjs-copy-vs-extend\\\/#webpage\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/angularjs-copy-vs-extend\\\/\",\"name\":\"AngularJS : copy vs extend | TO THE NEW Blog\",\"description\":\"Learn different utility functions of AngularJS such as copy and extend\",\"inLanguage\":\"en-US\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/angularjs-copy-vs-extend\\\/#breadcrumblist\"},\"author\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/sakshi\\\/#author\"},\"creator\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/sakshi\\\/#author\"},\"datePublished\":\"2014-02-06T22:11:41+05:30\",\"dateModified\":\"2017-08-03T14:09:28+05:30\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/\",\"name\":\"TO THE NEW Blog\",\"inLanguage\":\"en-US\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\"}}]}\n\t\t<\/script>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"AngularJS : copy vs extend | TO THE NEW Blog","description":"Learn different utility functions of AngularJS such as copy and extend","canonical_url":"https:\/\/2thenew.online\/blog\/angularjs-copy-vs-extend\/","robots":"max-image-preview:large","keywords":"","webmasterTools":{"miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/2thenew.online\/blog\/angularjs-copy-vs-extend\/#article","name":"AngularJS : copy vs extend | TO THE NEW Blog","headline":"AngularJS : copy vs extend","author":{"@id":"https:\/\/2thenew.online\/blog\/author\/sakshi\/#author"},"publisher":{"@id":"https:\/\/2thenew.online\/blog\/#organization"},"datePublished":"2014-02-06T22:11:41+05:30","dateModified":"2017-08-03T14:09:28+05:30","inLanguage":"en-US","commentCount":11,"mainEntityOfPage":{"@id":"https:\/\/2thenew.online\/blog\/angularjs-copy-vs-extend\/#webpage"},"isPartOf":{"@id":"https:\/\/2thenew.online\/blog\/angularjs-copy-vs-extend\/#webpage"},"articleSection":"AngularJS, Front End Development, angular development, AngularJS, AngularJS Development Company, copy, extend"},{"@type":"BreadcrumbList","@id":"https:\/\/2thenew.online\/blog\/angularjs-copy-vs-extend\/#breadcrumblist","itemListElement":[{"@type":"ListItem","@id":"https:\/\/2thenew.online\/blog#listItem","position":1,"name":"Home","item":"https:\/\/2thenew.online\/blog","nextItem":{"@type":"ListItem","@id":"https:\/\/2thenew.online\/blog\/category\/angular\/#listItem","name":"AngularJS"}},{"@type":"ListItem","@id":"https:\/\/2thenew.online\/blog\/category\/angular\/#listItem","position":2,"name":"AngularJS","item":"https:\/\/2thenew.online\/blog\/category\/angular\/","nextItem":{"@type":"ListItem","@id":"https:\/\/2thenew.online\/blog\/angularjs-copy-vs-extend\/#listItem","name":"AngularJS : copy vs extend"},"previousItem":{"@type":"ListItem","@id":"https:\/\/2thenew.online\/blog#listItem","name":"Home"}},{"@type":"ListItem","@id":"https:\/\/2thenew.online\/blog\/angularjs-copy-vs-extend\/#listItem","position":3,"name":"AngularJS : copy vs extend","previousItem":{"@type":"ListItem","@id":"https:\/\/2thenew.online\/blog\/category\/angular\/#listItem","name":"AngularJS"}}]},{"@type":"Organization","@id":"https:\/\/2thenew.online\/blog\/#organization","name":"TO THE NEW Blog","url":"https:\/\/2thenew.online\/blog\/"},{"@type":"Person","@id":"https:\/\/2thenew.online\/blog\/author\/sakshi\/#author","url":"https:\/\/2thenew.online\/blog\/author\/sakshi\/","name":"Sakshi Tyagi","image":{"@type":"ImageObject","@id":"https:\/\/2thenew.online\/blog\/angularjs-copy-vs-extend\/#authorImage","url":"https:\/\/secure.gravatar.com\/avatar\/a42c6deb088e18815dfcfed629c77b032d17bf973881421880b9c5e4ba713739?s=96&d=mm&r=g","width":96,"height":96,"caption":"Sakshi Tyagi"}},{"@type":"WebPage","@id":"https:\/\/2thenew.online\/blog\/angularjs-copy-vs-extend\/#webpage","url":"https:\/\/2thenew.online\/blog\/angularjs-copy-vs-extend\/","name":"AngularJS : copy vs extend | TO THE NEW Blog","description":"Learn different utility functions of AngularJS such as copy and extend","inLanguage":"en-US","isPartOf":{"@id":"https:\/\/2thenew.online\/blog\/#website"},"breadcrumb":{"@id":"https:\/\/2thenew.online\/blog\/angularjs-copy-vs-extend\/#breadcrumblist"},"author":{"@id":"https:\/\/2thenew.online\/blog\/author\/sakshi\/#author"},"creator":{"@id":"https:\/\/2thenew.online\/blog\/author\/sakshi\/#author"},"datePublished":"2014-02-06T22:11:41+05:30","dateModified":"2017-08-03T14:09:28+05:30"},{"@type":"WebSite","@id":"https:\/\/2thenew.online\/blog\/#website","url":"https:\/\/2thenew.online\/blog\/","name":"TO THE NEW Blog","inLanguage":"en-US","publisher":{"@id":"https:\/\/2thenew.online\/blog\/#organization"}}]},"og:locale":"en_US","og:site_name":"TO THE NEW BLOG","og:type":"blog","og:title":"AngularJS : copy vs extend | TO THE NEW Blog","og:description":"Learn different utility functions of AngularJS such as copy and extend","og:url":"https:\/\/2thenew.online\/blog\/angularjs-copy-vs-extend\/","og:image":"https:\/\/2thenew.online\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png","og:image:secure_url":"https:\/\/2thenew.online\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png","twitter:card":"summary","twitter:site":"@tothenew","twitter:title":"AngularJS : copy vs extend | TO THE NEW Blog","twitter:description":"Learn different utility functions of AngularJS such as copy and extend","twitter:image":"https:\/\/2thenew.online\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png"},"aioseo_meta_data":{"post_id":"11719","title":null,"description":"Learn different utility functions of AngularJS such as copy and extend","keywords":null,"keyphrases":null,"primary_term":null,"canonical_url":null,"og_title":"","og_description":"","og_object_type":"blog","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":"","og_custom_url":null,"og_article_section":"","og_article_tags":"","twitter_use_og":false,"twitter_card":"summary","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"Article","isEnabled":true},"graphs":[]},"schema_type":null,"schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":null,"robots_max_videopreview":null,"robots_max_imagepreview":"large","priority":null,"frequency":null,"local_seo":null,"limit_modified_date":false,"created":"2021-04-29 15:55:16","updated":"2024-02-29 09:27:24","focus_keyword":null,"additional_keywords":null,"truseo_locale":null,"ai":null,"breadcrumb_settings":null,"seo_analyzer_scan_date":null},"aioseo_breadcrumb":"<div class=\"aioseo-breadcrumbs\"><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/2thenew.online\/blog\" title=\"Home\">Home<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/2thenew.online\/blog\/category\/angular\/\" title=\"AngularJS\">AngularJS<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\tAngularJS : copy vs extend\n\t\t<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"https:\/\/2thenew.online\/blog"},{"label":"AngularJS","link":"https:\/\/2thenew.online\/blog\/category\/angular\/"},{"label":"AngularJS : copy vs extend","link":"https:\/\/2thenew.online\/blog\/angularjs-copy-vs-extend\/"}],"_links":{"self":[{"href":"https:\/\/2thenew.online\/blog\/wp-json\/wp\/v2\/posts\/11719","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/2thenew.online\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/2thenew.online\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/2thenew.online\/blog\/wp-json\/wp\/v2\/users\/65"}],"replies":[{"embeddable":true,"href":"https:\/\/2thenew.online\/blog\/wp-json\/wp\/v2\/comments?post=11719"}],"version-history":[{"count":0,"href":"https:\/\/2thenew.online\/blog\/wp-json\/wp\/v2\/posts\/11719\/revisions"}],"wp:attachment":[{"href":"https:\/\/2thenew.online\/blog\/wp-json\/wp\/v2\/media?parent=11719"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/2thenew.online\/blog\/wp-json\/wp\/v2\/categories?post=11719"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/2thenew.online\/blog\/wp-json\/wp\/v2\/tags?post=11719"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}