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. MongoDB的Find详解(一)

    1.指定返回的键 db.[documentName].find ({条件},{键指定}) 数据准备persons.json var persons = [{name:"jim",a ...

  2. 去duplicate的方法

    1.什么是duplicate,为什么要去除. 什么是duplicate:这是在建库的过程后,对已连有接头的DNA片段进行扩增,然后去接flowcell.之所以在建库后扩增,这是由于接flowcell的 ...

  3. ceph安装各种报错

    [ceph_deploy][ERROR ] RuntimeError: Failed to execute command: ceph-disk-activate –mark-init sysvini ...

  4. linux shell执行SQL脚本

    #!/bin/sh user="user" pass="pass" sqlplus -S $user/$pass select 1 from dual; exi ...

  5. 【BZOJ1854】游戏[SCOI2009](神奇贪心+并查集)

    这道题和今年GDKOI的Day2T2很像(然而gdkoi的题用网络流可以A,这道题只能拿30). 网址:http://www.lydsy.com/JudgeOnline/problem.php?id= ...

  6. 考勤助手ER图2.0版本所存在的问题

    如图所示,考勤助手ER图2.0版本中,缺少个人信箱这一问题虽然不具有逻辑层面的问题,但是它是不满足需求的,也就是说这样的设计无法满足小组对考勤助手最初的设计.其次,就设计层面来分析,弱联系是数据库查询 ...

  7. java arrays类学习

    java.util.Arrays类能方便地操作数组,它提供的所有方法都是静态的. 具有以下功能: (1)给数组赋值:通过fill方法. (2)对数组排序:通过sort方法,按升序. (3)比较数组:通 ...

  8. java中@Qualifier("string")是什么用法

    @Qualifier("XXX") Spring的Bean注入配置注解,该注解指定注入的Bean的名称,Spring框架使用byName方式寻找合格的bean,这样就消除了byTy ...

  9. review41

    套接字是基于TCP协议的网络通信. 基于UDP

  10. svn更新时忽略指定文件或文件夹

    选择一个收SVN控制的文件夹->右击->选择TortoiseSVN->更新至版本,就会出现   选择更新深度为工作副本,再选择项目,出现如图中所示的界面,把不想更新的文件或者文件夹前 ...