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. C语言math.h库函数中atan与atan2的区别

    源: C语言math.h库函数中atan与atan2的区别 C语言中的atan和atan2

  2. python对象类型----数字&字符串

    一数据类型:      float: 1.3e-3  1.3*10的负三次方 print (1.3e-3)    bin()  #转换为二进进制    oct() #转换为8进制    hex()#转 ...

  3. php数组函数-array_map()

    array_map()函数返回用户自定义函数作用后的数组.回调函数接受的参数 数目应该和传递给array_map()函数的数组数目一直. array_map(function,array1,array ...

  4. poj 1703 Find them, Catch them 【并查集 新写法的思路】

    题目地址:http://poj.org/problem?id=1703 Sample Input 1 5 5 A 1 2 D 1 2 A 1 2 D 2 4 A 1 4 Sample Output N ...

  5. 转-centos7下安装apache服务器httpd的yum方式安装

    转自Clement-Xu的csdn博客 http://blog.csdn.net/clementad/article/details/41620631   Apache在Linux系统中,其实叫“ht ...

  6. mongodb,redis简单学习

     2.mongodb安装配置简单学习                   配置好数据库路径就可以mongo命令执行交互操作了:先将服务器开起来:在开个cmd执行交互操作                 ...

  7. spark学习2-1(hive1.2安装)

    由于前面安装版本过老,导致学习过程中出现了很多问题,今天安装了一个新一点的版本.安装结束启动时遇到一点问题,记录在这里. 第一步:hive-1.2安装 通过WinSCP将apache-hive-1.2 ...

  8. 使用log4j2打印Log,log4j不能打印日志信息,log4j2不能打印日志信息,log4j和logj2,idea控制台信息乱码(文末)

    说来惭愧,今天就写了个"hello world",了解了一下log4j的日志. 本来是想在控制台打印个log信息,也是遇到坎坷重重,开始也没去了解log4j就来使用,log4j配置 ...

  9. RowSet

    import java.io.FileInputStream; import java.util.Properties; import javax.sql.rowset.JdbcRowSet; imp ...

  10. App测试经验分享之登录注册

    要诀 另外自己总结了一些要诀,仅供参考: 1)快:快速操作,营造冲突的场景,例如加载过程中返回键交互,快速点击登录按钮,快速切换菜单项,快速多次上下拉刷新 2)变:手机横竖屏.手机切换语言.手机调整字 ...