While using AngularJS, we come across some situation in which we need to copy one object to another object. In that case, we probably have two solutions: angular.copy() or angular.extend(). Lets see how they work differently.

1. angular.copy(source, destination) : It creates a deep copy of source object or array and assign it to destination where ‘destination’ is optional. By writing deep copy, we mean that a new copy of the referred object is made. For example:

var mySource = {'name' : 'sakshi', 'age' : '24', 'obj' :{'key':'value'}}
var myDest = {}
angular.copy(mySource, myDest);
console.log(myDest);

Output: {'name' : 'sakshi', 'age' : '24', 'obj' :{'key':'value'}}

If we check mySource.obj === myDest.obj , this will give false because both point to different objects. This is called as deep copying.

2. angular.extend(destination, src1, src2 …) : It creates a shallow copy of one or more sources provided and assign them to destination. For example:

var mySource1 = {'name' : 'neha', 'age' : '26', obj2 : {}}
var mySource2 = {'course' : 'MCA'}
var myDest = {}
angular.extend(myDest, mySource1,mySource2)
console.log(myDest);

Output: {name: "neha", age: "26", course: "MCA", obj2: Object}

Now we check mySource1.obj2 === myDest.obj2 , this will give true because both point to same reference of object. This is called as shallow copying.

NOTE : angular.copy() is slower than angular.extend()

来源:http://www.tuicool.com/articles/En6Jve

中文解释如下截图:

可参考:

http://docs.angularjs.cn/api/ng/function/angular.copy

http://docs.angularjs.cn/api/ng/function/angular.extend

http://www.cnblogs.com/xing901022/p/4934329.html

http://www.tuicool.com/articles/vM7r6v

angular : copy vs extend的更多相关文章

  1. angular.extend()和 angular.copy()的区别

    1.angular.copy angular.copy(source, [destination]);  // source: copy的对象. 可以使任意类型, 包括null和undefined. ...

  2. angular.js 的angular.copy 、 angular.extend 、 angular.merge

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  3. angular.extend vs angular.copy

    1.angular.copy angular.copy(source, [destination]);  // source: copy的对象. 可以使任意类型, 包括null和undefined. ...

  4. AngularJs angular.bind、angular.bootstrap、angular.copy

    angular.bind 返回一个调用self的函数fn(self代表fn里的this).可以给fn提供参数args(*).这个功能也被称为局部操作,以区别功能. 格式:angular.bind(se ...

  5. Angular - - angular.bind、angular.bootstrap、angular.copy

    angular.bind 返回一个调用self的函数fn(self代表fn里的this).可以给fn提供参数args(*).这个功能也被称为局部操作,以区别功能. 格式:angular.bind(se ...

  6. angular.copy()

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  7. angular.copy(source, destination)

    angular.copy(source, destination)只有1个参数,返回source的深拷贝有2个参数source的深拷贝复制给destination

  8. AngularJS方法 —— angular.copy

    描述: 复制一个对象或者一个数组(好吧,万物皆对象,数组也是一个对象). 如果省略了destination,一个新的对象或数组将会被创建出来: 如果提供了destination,则source对象中的 ...

  9. angular JS中 ‘=’与angular.copy的区别

    先来看代码: <b>{{test1}}</b> <input type="text" ng-model="test2" title ...

随机推荐

  1. 【Head First Servlets and JSP】笔记17:JSP所生成的servlet相关问题

    1.容器根据你所写的JSP生成一个类, /* * Generated by the Jasper component of Apache Tomcat * Version: Apache Tomcat ...

  2. JSP笔记05——生命周期(转)

    原始内容:https://www.tutorialspoint.com/jsp/jsp_life_cycle.htm 在这一章中,我们将讨论JSP的生命周期. 理解JSP低层次功能的关键在于——理解它 ...

  3. MySQL运维问题集锦

    1.莫名的慢查询问题.解决思路:http://hidba.org/?spm=5176.153233.793262.6.d75LDx&p=1119

  4. Linux 一键安装 webmin/virtualmin

    Webmin是一个可运行于Linux/freebsd的web界面的主机管理系统,而Virtualmin是一个基于Webmin的虚拟主机管理模块. webmin官方站: http://www.webmi ...

  5. 用vim写python脚本的自动缩进格式设置

  6. HTML文本/文字竖直方向/纵向显示

    HTML vertical text (Safari, Firefox, Chrome, and Opera) .vText { -moz-transform: rotate(-90deg) tran ...

  7. 用简单的反射优化代码(动态web项目)

    在动态web项目中,没有使用框架时,只是简单的jsp访问servlet实现增删改查, 无论是哪个方法都需要经过Servlet中的doGet()方法或doPost()方法,我们可以在链接中附带参数进行区 ...

  8. c++中的函数对象

    头文件wuyong.h: #pragma once #include<iostream> using namespace std; template<typename T> s ...

  9. 二叉查找树--java

    package com.test.tree; public class BinarySearchTree<T extends Comparable<? super T>> { ...

  10. 解决 Amoeba连接mysql出错 解决方案

    今天配置mysql的主从复制 用到了Amoeba.从安装到启动服务,我深深地体会到学运维的不易. 首先是  安装错误  的解决,连接错误  的兄弟可以直接往下拉. 安装错误 1.出现 JAVA_HOM ...