复制一个对象或者一个数组(好吧,万物皆对象,数组也是一个对象)。
如果省略了destination,一个新的对象或数组将会被创建出来;
如果提供了destination,则source对象中的所有元素和属性都会被复制到destination中;
如果source不是对象或数组(例如是null或undefined), 则返回source;
如果source和destination类型不一致,则会抛出异常。
<!DOCTYPE html>
<html lang="en" ng-app="app">
<head>
<meta charset="UTF-8">
<title>copy</title>
<script type="text/javascript" src="js/angular.min.js"></script>
</head>
<body ng-controller="cc">
</body>
<script type="text/javascript">
angular.module('app',[])
.controller('cc',function ($scope) {
$scope.user={
name:'lili',
age:'12'
}
//1.提供destination
$scope.userCopy={}; //source和destination类型要一致
angular.copy($scope.user,$scope.userCopy);
console.log($scope.userCopy); //{name:'lili',age:'12'}
//2.source和destination一样,则会报错
console.log(angular.copy($scope.user,$scope.user));//报错
//3.不提供destination
$scope.mm=angular.copy($scope.user);
console.log($scope.mm); //{name:'lili',age:'12'}
//4.
var test;
console.log(angular.copy(test)); //undefined
//5.
var te=null;
console.log(angular.copy(te)); //null
})
</script>
</html>