在angular.copy()和assignment(=)之间有什么区别?

时间:2022-07-26 21:15:58

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.

我给$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(=)之间的区别。

4 个解决方案

#1


47  

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()执行一个深层复制(cf)。“克隆”)的参数——本质上是创建一个新对象——而使用赋值运算符=仅分配引用。

Thus in the latter case, if you we're to change something in $scope.master you would also change context.

因此在后一种情况下,如果我们要改变$作用域。掌握你也会改变环境。

Cheers,

欢呼,

#2


10  

= 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.master or vice versa.

使用=意味着改变contexta的属性将改变$scope的相应属性。主,反之亦然。

Using angular.copy() the two objects would remain seperate and changes would not reflect on each other.

使用angular.copy()这两个对象将保持分离,并且更改不会相互反映。

#3


3  

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)时,=和角度。复制与任何赋值相同,结果是复制变量的值。

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中操作对象时,=将对现有对象的引用赋给变量和角度。复制是复制,这意味着创建具有相同属性和值的新对象,并将新对象的引用分配给变量。

#4


2  

Simply

简单的

angular.copy() is same as .clone() of jquery which create & returns same object copy with dept. (call by value)

copy()与jquery的.clone()相同,后者创建并返回与dept.(按值调用)相同的对象拷贝。

= 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的值将被赋给a,但如果a和b都是数组,那么a中的变化将反映在b中,反之亦然。

#1


47  

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()执行一个深层复制(cf)。“克隆”)的参数——本质上是创建一个新对象——而使用赋值运算符=仅分配引用。

Thus in the latter case, if you we're to change something in $scope.master you would also change context.

因此在后一种情况下,如果我们要改变$作用域。掌握你也会改变环境。

Cheers,

欢呼,

#2


10  

= 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.master or vice versa.

使用=意味着改变contexta的属性将改变$scope的相应属性。主,反之亦然。

Using angular.copy() the two objects would remain seperate and changes would not reflect on each other.

使用angular.copy()这两个对象将保持分离,并且更改不会相互反映。

#3


3  

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)时,=和角度。复制与任何赋值相同,结果是复制变量的值。

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中操作对象时,=将对现有对象的引用赋给变量和角度。复制是复制,这意味着创建具有相同属性和值的新对象,并将新对象的引用分配给变量。

#4


2  

Simply

简单的

angular.copy() is same as .clone() of jquery which create & returns same object copy with dept. (call by value)

copy()与jquery的.clone()相同,后者创建并返回与dept.(按值调用)相同的对象拷贝。

= 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的值将被赋给a,但如果a和b都是数组,那么a中的变化将反映在b中,反之亦然。