我应该使用Angular.copy()还是_.clone()?

时间:2022-07-26 21:16:04

I'm working on a project that has both Angular and Underscore as a dependency.

我正在做一个项目,这个项目的角和下划线都是相互依赖的。

When I need to create a copy of an object, depending on my mood at the time, I might use angular.copy() or _.clone()

当我需要创建一个对象的副本时,根据我当时的心情,我可以使用angular.copy()或_.clone()

It occurs to me that one of these methods is probably more fast/reliable/robust than the other.

我突然想到,其中一种方法可能比另一种方法更快/更可靠/更健壮。

Are there any known issue with either of these functions that make it better or worse to use than the other, assuming both libraries are already included?

假设这两个库都包含在内,那么这两个函数中是否有任何已知的问题使其比其他函数更好或更差?

2 个解决方案

#1


40  

Regarding your question: angular.copy and _.clone are different. It's not a question of which is better, it is about what you need as @Kevin B stated in the comments.

关于你的问题:角。复制和_。克隆是不同的。这不是哪个更好的问题,而是像@Kevin B在评论中所说的那样,你需要什么。

angular.extend() on the other hand, is a shallow copy akin to _.clone

另一方面,扩展()是类似于_.clone的浅拷贝。

Angular.copy vs Angular.extend

角。复制vs Angular.extend

Performance wise, i'm not sure which is better, but for opinions sake, i'm opposed to including libraries into the global scope (underscore) with any angular app, as usually these things are written as angular modules. angular.copy/angular.extend would win in this case.

性能方面,我不确定哪一种更好,但是为了表达观点,我反对将库包括到全局范围(下划线)和任意角的应用程序,通常这些东西都是作为角模块编写的。angular.copy /角。在这种情况下,扩展将会胜出。

Shallow/Deep Copy:

浅或深拷贝:

Its very simple that if the object has only primitive fields, then obviously you will go for shallow copy but if the object has references to other objects, then based on the requiement, shallow copy or deep copy should be chosen. What I mean here is, if the references are not modified anytime, then there is no point in going for deep copy. You can just opt shallow copy. But if the references are modified often, then you need to go for deep copy. Again there is no hard and fast rule, it all depends on the requirement.

很简单,如果对象只有原始字段,那么显然您会选择浅拷贝,但是如果对象有对其他对象的引用,那么根据请求,应该选择浅拷贝或深拷贝。我的意思是,如果引用在任何时候都没有修改,那么就没有必要进行深度复制。你可以选择浅拷贝。但是如果引用经常被修改,那么您需要进行深度复制。同样,也没有硬性的规则,一切都取决于需求。

Source

#2


3  

We had some bug reports confirming that using angular.copy does create empty objects on some Windows mobile phones. So if you need to support any version of IE on mobile, don't use angular.copy! Allegedly this bug has been fixed by microsoft, but nevertheless we had to deal with it...

我们有一些错误报告证实使用角。复制确实在一些Windows手机上创建了空对象。所以如果你需要支持任何版本的IE在移动,不要使用angular.copy!据说这个bug已经被微软修复了,但是我们不得不处理它……

Actually, you can as well use Object.assign()...

实际上,您也可以使用Object.assign()…

Docs: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/assign

文档:https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/assign

Further examples: https://googlechrome.github.io/samples/object-assign-es6/

进一步的例子:https://googlechrome.github.io/samples/object-assign-es6/

I know it says no IE, but i tried it on my IE11 and it works...

我知道它说没有IE,但我在我的IE11上试过,它是有效的……

#1


40  

Regarding your question: angular.copy and _.clone are different. It's not a question of which is better, it is about what you need as @Kevin B stated in the comments.

关于你的问题:角。复制和_。克隆是不同的。这不是哪个更好的问题,而是像@Kevin B在评论中所说的那样,你需要什么。

angular.extend() on the other hand, is a shallow copy akin to _.clone

另一方面,扩展()是类似于_.clone的浅拷贝。

Angular.copy vs Angular.extend

角。复制vs Angular.extend

Performance wise, i'm not sure which is better, but for opinions sake, i'm opposed to including libraries into the global scope (underscore) with any angular app, as usually these things are written as angular modules. angular.copy/angular.extend would win in this case.

性能方面,我不确定哪一种更好,但是为了表达观点,我反对将库包括到全局范围(下划线)和任意角的应用程序,通常这些东西都是作为角模块编写的。angular.copy /角。在这种情况下,扩展将会胜出。

Shallow/Deep Copy:

浅或深拷贝:

Its very simple that if the object has only primitive fields, then obviously you will go for shallow copy but if the object has references to other objects, then based on the requiement, shallow copy or deep copy should be chosen. What I mean here is, if the references are not modified anytime, then there is no point in going for deep copy. You can just opt shallow copy. But if the references are modified often, then you need to go for deep copy. Again there is no hard and fast rule, it all depends on the requirement.

很简单,如果对象只有原始字段,那么显然您会选择浅拷贝,但是如果对象有对其他对象的引用,那么根据请求,应该选择浅拷贝或深拷贝。我的意思是,如果引用在任何时候都没有修改,那么就没有必要进行深度复制。你可以选择浅拷贝。但是如果引用经常被修改,那么您需要进行深度复制。同样,也没有硬性的规则,一切都取决于需求。

Source

#2


3  

We had some bug reports confirming that using angular.copy does create empty objects on some Windows mobile phones. So if you need to support any version of IE on mobile, don't use angular.copy! Allegedly this bug has been fixed by microsoft, but nevertheless we had to deal with it...

我们有一些错误报告证实使用角。复制确实在一些Windows手机上创建了空对象。所以如果你需要支持任何版本的IE在移动,不要使用angular.copy!据说这个bug已经被微软修复了,但是我们不得不处理它……

Actually, you can as well use Object.assign()...

实际上,您也可以使用Object.assign()…

Docs: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/assign

文档:https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/assign

Further examples: https://googlechrome.github.io/samples/object-assign-es6/

进一步的例子:https://googlechrome.github.io/samples/object-assign-es6/

I know it says no IE, but i tried it on my IE11 and it works...

我知道它说没有IE,但我在我的IE11上试过,它是有效的……