angular : copy vs extend
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的更多相关文章
- angular.extend()和 angular.copy()的区别
1.angular.copy angular.copy(source, [destination]); // source: copy的对象. 可以使任意类型, 包括null和undefined. ...
- angular.js 的angular.copy 、 angular.extend 、 angular.merge
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- angular.extend vs angular.copy
1.angular.copy angular.copy(source, [destination]); // source: copy的对象. 可以使任意类型, 包括null和undefined. ...
- AngularJs angular.bind、angular.bootstrap、angular.copy
angular.bind 返回一个调用self的函数fn(self代表fn里的this).可以给fn提供参数args(*).这个功能也被称为局部操作,以区别功能. 格式:angular.bind(se ...
- Angular - - angular.bind、angular.bootstrap、angular.copy
angular.bind 返回一个调用self的函数fn(self代表fn里的this).可以给fn提供参数args(*).这个功能也被称为局部操作,以区别功能. 格式:angular.bind(se ...
- angular.copy()
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- angular.copy(source, destination)
angular.copy(source, destination)只有1个参数,返回source的深拷贝有2个参数source的深拷贝复制给destination
- AngularJS方法 —— angular.copy
描述: 复制一个对象或者一个数组(好吧,万物皆对象,数组也是一个对象). 如果省略了destination,一个新的对象或数组将会被创建出来: 如果提供了destination,则source对象中的 ...
- angular JS中 ‘=’与angular.copy的区别
先来看代码: <b>{{test1}}</b> <input type="text" ng-model="test2" title ...
随机推荐
- mybatis使用注意的细节
1.mybatis对sql执行后会对结果进行封装,如果没有返回任何记录,只是封装后的对象没有值,而对象并不为空null: (这个问题疏忽坑了两次,在对返回数组结果进行判断的时候,我用的if(Array ...
- imx6q 添加intel PCIE网卡
TQ_IMX6Q开发板移植rtl8168-PCIE转千兆网卡 一.配置内核选项PCIE总线驱动支持 默认的内核配置可能没有把PCIE的总线驱动编入内核,所以需要确认是否把驱动编译到了内核里面. 配置好 ...
- window7 3G/4G拨号操作
Win7系统Modem拨号操作指导:https://wenku.baidu.com/view/bb855b1dc77da26925c5b0e1.html 拨号上网设置APN,拨号号码,帐号和密码:ht ...
- 在Java中调用Python代码
极少数时候,我们会碰到类似这样的问题:与A同学合作写代码, A同学只会写Python,不熟悉Java ,而你只会写Java不擅长Python,并且发现难以用Java来重写对方的代码,这时,就不得不想方 ...
- HTML5_CSS3实现iOS Path菜单
在线演示 本地下载
- 九、goroutine和channel
进程和线程 A)进程是程序在操作系统中的一次执行过程,系统进行资源分配和调度的一个独立单元 B)线程是进程的一个执行实体,是CPU调度和分派的基本单位,它是比进程更小的能独立运行的基本单位 C)一个进 ...
- uboot无法引导uImage错误及其解决方法
先编译友善提供的linux内核: make ARCH=arm mini2440_defconfigmake CROSS_COMPILE=arm-linux- uImage 在arch/arm/boot ...
- Go Redis 开发
redigo库来实现redis的操作:https://github.com/gomodule/redigo Redis常用操作 示例代码: package main import ( "gi ...
- Docker Compose yml
Wordpress + Mysql version: '3' services: db: image: mysql:latest volumes: - db_data:/var/lib/mysql e ...
- RHEL 7 安装 ngnix
安装ngnix yum install -y make apr* autoconf automake curl curl-devel gcc gcc-c++ gtk+-devel zlib-devel ...