[英]What is the difference between angular.copy() and an assignment (=)?
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 user
values to $scope.master
.我已将
user
值分配给$scope.master
。
Now i am seeing angular.copy() .现在我看到了angular.copy() 。 So I wrote the code with 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(=)
之间的区别。
As can be read here angular.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()
执行参数的深层复制(参见“克隆”) - 本质上创建一个新对象 - 而使用赋值运算符=
只是分配引用的。
Thus in the latter case, if you we're to change something in $scope.master
you would also change context
.因此,在后一种情况下,如果您要更改
$scope.master
某些内容,您也会更改context
。
Cheers,干杯,
=
represents a reference whereas angular.copy()
creates a new object as a deep copy. =
代表一个引用,而angular.copy()
创建一个新对象作为深拷贝。
Using =
would mean that changing a property of context
would change the corresponding property of $scope.master
or vice versa.使用
=
意味着更改context
的属性将更改$scope.master
的相应属性,反之亦然。
Using angular.copy()
the two objects would remain seperate and changes would not reflect on each other.使用
angular.copy()
两个对象将保持独立,更改不会相互反映。
When you manipulate primitive types (like int) in Javascript, =
and angular.copy
are 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.copy
is 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
正在复制,这意味着创建一个具有相同属性和值的新对象,并将新对象的引用分配给变量。
Simply简单地
angular.copy()
is same as .clone()
of jquery which create & returns same object copy with dept. angular.copy()
是相同.clone()
其创建与&部门返回相同的对象副本的jquery的。 (call by value) (按值调用)
=
it does assign the value with its reference value(call by reference), =
它确实用它的参考值分配了值(按引用调用),
a = b
in this a will be b
value is assigned to a
, but if both a
& b
are array then changes in a
will reflect in b
& vice versa. a = b
在这个将b
值被分配给a
,但如果两者a
& b
是阵列然后在改变a
将在反映b
&反之亦然。
In assignment we share the reference of the object.在赋值中,我们共享对象的引用。 when we use angular.copy we create a new reference point with the same object details.
当我们使用 angular.copy 时,我们创建了一个具有相同对象细节的新参考点。
var user1={name:'hello'};
The object {name:'hello'}
has a reference point(let's say 123, which is saved in user1; when we write对象
{name:'hello'}
有一个引用点(比如 123,保存在 user1 中;当我们写
var user2=var user1; //reference point of user2 is also 123
user2.name="world"; //we update the object in 123
console.log(user1.name); //answer is "world" because reference object is updated
In case you don't want to update user1 when you change something in user2 we have to create a copy.如果您不想在更改 user2 中的某些内容时更新 user1,我们必须创建一个副本。 we can do it like
我们可以这样做
var user2=angular.copy(user1);
or或者
var user2=Object.assign({},user1);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.