Lodash - .extend()/ .assign()和.merge()之间的区别 [英] Lodash - difference between .extend() / .assign() and .merge()

查看:134
本文介绍了Lodash - .extend()/ .assign()和.merge()之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Lodash 库中,有人可以更好地解释合并扩展/分配

In the Lodash library, can someone provide a better explanation of merge and extend / assign.

这是一个简单的问题,但答案却避开了我。

Its a simple question but the answer evades me nonetheless.

推荐答案

以下是扩展 / assign <的方式/ code>有效:对于源中的每个属性,将其值按原样复制到目标。如果属性值本身是对象,则不会对其属性进行递归遍历。整个对象将从源获取并设置到目的地。

Here's how extend/assign works: For each property in source, copy its value as-is to destination. if property values themselves are objects, there is no recursive traversal of their properties. Entire object would be taken from source and set in to destination.

以下是 merge 的工作方式:对于每个属性source,检查该属性是否为对象本身。如果它然后递归下去并尝试将子对象属性从源映射到目标。基本上我们将对象层次结构从源合并到目标。对于 extend / assign ,它是从源到目标的简单的一级属性副本。

Here's how merge works: For each property in source, check if that property is object itself. If it is then go down recursively and try to map child object properties from source to destination. So essentially we merge object hierarchy from source to destination. While for extend/assign, it's simple one level copy of properties from source to destination.

这是一个简单的JSBin,可以使这个结晶清晰:
http:/ /jsbin.com/uXaqIMa/2/edit?js,console

Here's simple JSBin that would make this crystal clear: http://jsbin.com/uXaqIMa/2/edit?js,console

以下是更精细的版本,其中包括示例中的数组:
http://jsbin.com/uXaqIMa/1/edit?js,console

Here's more elaborate version that includes array in the example as well: http://jsbin.com/uXaqIMa/1/edit?js,console

这篇关于Lodash - .extend()/ .assign()和.merge()之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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