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. 收集整理的awk用法小结

    awk 用法:awk ‘ pattern {action} ‘ 变量名 含义 ARGC 命令行变元个数 ARGV 命令行变元数组 FILENAME 当前输入文件名 FNR 当前文件中的记录号 FS 输 ...

  2. 【Tech】mac下svn和scp使用笔记

    1.命令行从svn下载代码 mac本身自带svn,所以使用非常简单,在本地创建代码存放的文件夹,然后cd到该文件夹下,运行: svn checkout svn://ip地址/文件路径 . 然后出现要求 ...

  3. PHP 面向对象及Mediawiki 框架分析(二)

    mediaHandler可以理解为处理media文件的 /includes/filerepo/file/File.php /** * Get a MediaHandler instance for t ...

  4. ZK服务管理中心

    ZK基础类及服务的注册与发现: package top.letsgogo.util; import org.I0Itec.zkclient.ZkClient; import org.I0Itec.zk ...

  5. Nginad Server安装

    前言 Nginad是一个基于php的开源项目,它既可以作为静态配置的Ad Server,也可以作为动态的RTB Exchange使用.代码结构比较直接明了,挺适合用作学习的.本文如果有理解错误的地方, ...

  6. ZooKeeper-znode概念与使用

    可以将ZK看作一个具有高可用性特征的文件系统.这个文件系统中没有文件和目录,而是统一使用节点(znode)的概念,称为znode.znode既可以作为保存数据的容器(如同文件),也可以作为保存其他zn ...

  7. 批量测试邮箱登录python脚本

    ---恢复内容开始--- #!/usr/bin/env python #-*- coding:utf-8 -*- import smtplib import sys import time impor ...

  8. Java JDK、Tomcat、Eclipse环境配置

    Java 下载地址:http://www.oracle.com/ 根据提示一步一步进行安装,通常安装到C:\Program Files\Java,包含: 环境变量配置: JAVA_HOME:C:\Pr ...

  9. Unity3D-UGUI图集打包与动态使用(TexturePacker)

    参考地址: http://blog.csdn.net/cjsen/article/details/52487706 今天做项目大佬看我在做图集,就跟我说可以用工具打包图集,也就是TexturePack ...

  10. sikiA计划问题记录

    1.有时候在场景中创建光源会有一条虚线 解决方法:禁用锯齿 edit - project setting - quality - anti aliasing