我应该使用Angular.copy()或_.clone()? [英] Should I Use Angular.copy() or _.clone()?

查看:156
本文介绍了我应该使用Angular.copy()或_.clone()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作有两个角,并强调作为依赖的一个项目。

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

当我需要创建一个对象的副本,这取决于我的心情的时候,我可能会使用 angular.copy() _.clone()

When I need to create a copy of an object, depending on my mood at the time, I might use angular.copy() or _.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?

推荐答案

关于你的问题:的angular.copy和_.clone是不同的。这不是一个问题,这是更好的,这是你需要什么作为@Kevin乙方在注释中规定。

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.

angular.extend(),另一方面,是一种浅拷贝类似于_.clone

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

Angular.copy VS Angular.extend

性能明智的,我不知道这是更好的,但是对于意见的缘故,我反对包括图书馆成为全球范围内(下划线)与任意角度的应用程序,因为通常这些东西写成的angular模块。 angular.copy / 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.

浅/深复制

它非常简单,如果对象只有原始的领域,那么显然你会去浅拷贝,但如果对象具有其他对象的引用,然后根据requiement,浅复制或深拷贝应选择。我的意思是在这里,如果引用不随时修改,再有就是去为深拷贝是没有意义的。你可以只选择浅拷贝。但是,如果引用经常被修改,那么你就需要去深层复制。同样没有硬性规定,这一切都取决于需求。

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.

来源

这篇关于我应该使用Angular.copy()或_.clone()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆