[英]array.concat vs angular.copy
I've noticed that [].concat()
performs similarly to angular.copy()
with arrays. 我注意到
[].concat()
angular.copy()
与数组的angular.copy()
类似。 For instance, 例如,
var x = [5, 2];
var y = [].concat(x);
// y = [5, 2]
x = [2, 3];
// y = [5, 2]
var z = angular.copy(x);
// z = [2, 3];
x = [6, 9];
// z = [2, 3];
Is there a key difference between [].concat()
and angular.copy(src,[dest])
? [].concat()
和angular.copy(src,[dest])
之间有关键区别吗?
angular.copy performs a deep copy of the source and places it on the destination ( Both the arrays of source and dest and its contents, even reference types, points to different reference location ). angular.copy执行源的深层复制并将其放置在目标上( source和dest的数组及其内容,甚至引用类型,都指向不同的引用位置 )。 But when you do [].concat ( Both the arrays of source and dest points to different reference and its reference type contents points to the same reference ), it just returns a new array, so only think that is similar in using both
angular.copy
and [].concact
in your example is that it assigns a new reference of the array object to the lhs variable. 但是,当您执行[] .concat( source和dest的数组都指向不同的引用,而其引用类型的内容都指向相同的引用 )时,它只是返回一个新数组,因此仅认为在使用
angular.copy
这是相似的angular.copy
在您的示例中, angular.copy
和[].concact
是将数组对象的新引用分配给lhs变量。
But consider the situation where you have array of objects. 但是考虑一下您拥有对象数组的情况。
$scope.arr = [{name:'name1'}, {name:'name2'}, {name:'name3'}, {name:'name4'}];
$scope.arrConcat = [].concat($scope.arr); //Get a new array
$scope.arrCopy = angular.copy($scope.arr); //Get a new copy
//Now change the name property of one of the item
$scope.arr[3].name="Test";
//Now see who all have been changed
console.log($scope.arr[3].name); //Of course will output "Test"
console.log($scope.arrConcat[3].name); //Will also output "Test" because the new array items still holds the same reference of the objects.
console.log($scope.arrCopy[3].name); //Will output name4 because this contains another reference which holds the copy of the value of the object at index 3 from source array
//Push something on to the original array
$scope.arr.push({name:'name5'});
//You will see that new item is not available here, which is exactly the behaviour that you are seeing in your case because both the arrConcat and arrCopy holds its own version of array through the items in arrConcat and arr are from the same reference.
console.log($scope.arrConcat);
console.log($scope.arrCopy);
So only thing is that in your case [].concat
is kind of a convenient method to get a copy of the source array since your array just has primitives, so no issues. 因此,唯一的情况是,在您的情况下
[].concat
是一种获取源数组副本的便捷方法,因为您的数组仅具有基元,因此没有问题。
http://plnkr.co/edit/06zLM8g34IDBLUmPtwV2?p=preview http://plnkr.co/edit/06zLM8g34IDBLUmPtwV2?p=preview
var x = [5, 2];
var y = [].concat(x);
// y = [5, 2]
var x = [5, [5, 2]];
var y = [].concat(x);
// y = [5, 2]
Check this out, [].copy() never does a deep copy unlike angular.copy() 看看这个,[]。copy()永远不会像angular.copy()那样进行深层复制
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.