Javascript angular.copy() 和赋值 (=) 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29749433/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the difference between angular.copy() and an assignment (=)?
提问by Ramesh Rajendran
I want to assign some values when a button click event happens via event parameter:
当按钮单击事件通过事件参数发生时,我想分配一些值:
$scope.update = function(context) {
$scope.master = context;
};
I have assigned uservalues to $scope.master.
我已将user值分配给 $scope.master.
Now i am seeing angular.copy(). So I wrote the code with angular.copy.
现在我看到了angular.copy()。所以我用angular.copy写了代码。
$scope.update = function(context) {
$scope.master = angular.copy(context)
};
Both are doing same, so what is the difference? Please tell me about the difference between angular.copy()and equal(=).
两者都在做同样的事情,那么有什么区别呢?请告诉我angular.copy()和之间的区别equal(=)。
回答by Anders R. Bystrup
As can be read hereangular.copy()performs a deep copy (cf. "clone") of the argument - essentially creating a new object - whereas using the assignment operator =just assigns reference's.
正如可以在此处阅读的那样angular.copy()执行参数的深层复制(参见“克隆”) - 本质上是创建一个新对象 - 而使用赋值运算符=只是分配reference的。
Thus in the latter case, if you we're to change something in $scope.masteryou would also change context.
因此,在后一种情况下,如果您要更改某些内容,$scope.master您也会更改context.
Cheers,
干杯,
回答by Asher
=represents a reference whereas angular.copy()creates a new object as a deep copy.
=代表一个引用,而angular.copy()创建一个新对象作为深拷贝。
Using =would mean that changing a property of contextwould change the corresponding property of $scope.masteror vice versa.
使用=意味着改变 的属性context会改变相应的属性,$scope.master反之亦然。
Using angular.copy()the two objects would remain seperate and changes would not reflect on each other.
使用angular.copy()这两个对象将保持独立,更改不会相互反映。
回答by tomaoq
When you manipulate primitive types (like int) in Javascript, =and angular.copyare the same as any assignment results in copying the value of the variable.
当您在 Javascript 中操作原始类型(如 int)时,=与angular.copy复制变量值的任何赋值结果相同。
When you manipulate objects in Javascript, =assign a reference to the existing object to the variable and angular.copyis copying, that means creating a new object with the same properties and values and assigning the new object's reference to the variable.
当您在 Javascript 中操作对象时,=将现有对象的引用分配给变量并angular.copy进行复制,这意味着创建一个具有相同属性和值的新对象,并将新对象的引用分配给变量。
回答by Pankaj Parkar
Simply
简单地
angular.copy()is same as .clone()of jquery which create & returns same object copy with dept. (call by value)
angular.copy()与.clone()jquery相同,它使用 dept 创建并返回相同的对象副本。(按值调用)
=it does assign the value with its reference value(call by reference),
=它确实为其分配了参考值(按引用调用),
a = bin this a will be bvalue is assigned to a, but if both a& bare array then changes in awill reflect in b& vice versa.
a = b在此 a 将被b赋值给a,但如果两个a&b都是数组,则 in 的变化a将反映在b& 反之亦然。

