首页 > 其他分享 >Lodash 中 assign,extend 和 merge 的区分

Lodash 中 assign,extend 和 merge 的区分

时间:2023-02-06 11:47:57浏览次数:46  
标签:Lodash extend 对象 object merge assign 属性

简介

我们经常在他人的代码中看见 assignextendmerge 函数,这三个函数用起来很相似,都是兼并源对象的属性到目的对象中。

既然都是兼并对象,为何还分三个差别的函数呢?它们之间到底有什么区分呢?

assign(object, [sources])

我们先看看官方网站上面的定义:

Assigns own enumerable string keyed properties of source objects to the destination object. Source objects are applied from left to right. Subsequent sources overwrite property assignments of previous sources.

把源对象(sources)的属性分配到目的对象(object),源对象会从左往右地挪用,背面对象的属性会掩盖前面的。

看看下面的例子:

assign({}, { a: 1 }, { b: 2 });
// { a: 1, b: 2 }

// 背面的 { a: 2 } 把前面的 { a: 1 } 掩盖了
assign({}, { a: 1 }, { b: 2 }, { a: 2 });
// { a: 2, b: 2 }

// 视察下面两个例子,假如属性值为 object,背面的值会掩盖前面的值
assign(
  {},
  { a: 1 },
  { b: { c: 2, d: 3 } }
)
// { a: 1, b: { c: 2, d: 3 } }

assign(
  {},
  { a: 1 },
  { b: { c: 2, d: 3 } },
  { b: { e: 4 } }
)
// { a: 1, b: { e: 4 } }

// `assign` 函数会疏忽原型链上的属性。
function Foo() { this.c = 3; }
Foo.prototype.d = 4;
assign({ a: 1 }, new Foo());
// { a: 1, c: 3 }

// `assign` 会修正本来的对象
var test = { a: 1 };
assign(test, { b: 2 }); // { a: 1, b: 2 }
console.log(test);      // { a: 1, b: 2 }

extend(object, [sources])

在 3.x 版本中,extend 是 assign 的别号,它们的作用是如出一辙的。
在 4.x 版本中,extend 是 assignIn 的别号,和 assign 有点区分。

官方定义以下:

This method is like _.assign except that it iterates over own and inherited source properties.

在上面的例子中,我们晓得 assign 函数不会把原型链上的属性兼并到目的对象,而 extend 或 assignIn 函数则会!

// Important !! this is Lodash 4.x !!

// 把源对象原型链上的属性也兼并到目的对象上!
function Foo() { this.c = 3; }
Foo.prototype.d = 4;
extend({ a: 1 }, new Foo());
// { a: 1, c: 3, d: 4 }

merge(object, [sources])

我们看看 merge 函数的定义:

This method is like _.assign except that it recursively merges own and inherited enumerable string keyed properties of source objects into the destination object. Source properties that resolve to undefined are skipped if a destination value exists. Array and plain object properties are merged recursively.Other objects and value types are overridden by assignment. Source objects are applied from left to right. Subsequent sources overwrite property assignments of previous sources.

merge 也和 assign 相似,差别的处所在于 merge 碰到雷同属性的时刻,假如属性值为纯对象(plain object)或许鸠合(collection)时,不是用背面的属性值去掩盖前面的属性值,而是会把前后两个属性值兼并。
假如源对象的属性值为 undefined,则会疏忽该属性。

assign(
  {},
  { a: 1 },
  { b: { c: 2, d: 3} },
  { b: { e: 4 } }
)
// { a: 1, b: { e: 4 } }
merge(
  {},
  { a: 1 },
  { b: { c: 2, d: 3} },
  { b: { e: 4 } }
)
// { a: 1, b: { c: 2, d: 3, e: 4 } }

// 兼并鸠合
var users = {
  'data': [{ 'user': 'barney' }, { 'user': 'fred' }]
};
var ages = {
  'data': [{ 'age': 36 }, { 'age': 40 }]
};
merge({}, users, ages)
// { data: [ { user: 'barney', age: 36 }, { user: 'fred', age: 40 } ] }

// merge 函数会修正本来的对象!
merge(users, ages)
console.log(users) // { data: [ { user: 'barney', age: 36 }, { user: 'fred', age: 40 } ]

总结

雷同之处

  • 都可以用来兼并对象

  • 都邑修正本来的对象 (假如本来的对象是作为函数的第一个参数的话)

差别之处

  • assign 函数不会处置惩罚原型链上的属性,也不会兼并雷同的属性,而是用背面的属性值掩盖前面的属性值

  • extend

    • 3.x 版本中和 assign 一样

    • 4.x 版本中会兼并原型链上的属性

  • merge 碰到雷同属性名的时刻,假如属性值是纯对象或鸠合的时刻,会兼并属性值

出处

http://scarletsky.github.io/2016/04/02/a…

参考资料

https://lodash.com/docs
http://stackoverflow.com/questions/19965…

标签:Lodash,extend,对象,object,merge,assign,属性
From: https://www.cnblogs.com/ygyy/p/17094863.html

相关文章

  • 49面向对象-继承extends
    packagechapter04;publicclassJava12_Object_Extends{publicstaticvoidmain(String[]args){//TODO面向对象-Extends-继承//面向对象......
  • jQuery中的 $.extend() 和 $.fn.extend()
    jQuery中的$.extend()和$.fn.extend()ANGWH于2020-05-2406:39:59发布注意:$.extend是为jQuery类添加添加类方法,用$.调用(类似$.ajax),$.fn.extend则是为jQuery对......
  • [LeetCode]Merge Intervals
    Question本题难度Hard。排序+双指针【复杂度】时间O(Nlog(N))空间O(N)【思路】先按照每个元素的​​​start​​​从小到大进行排序。然后利用双指针法,设置区间​​......
  • 归并排序(Merge Sort)
    一、算法概述1.1算法分类十种常见排序算法可以分为两大类:比较类排序:通过比较来决定元素间的相对次序,由于其时间复杂度不能突破O(nlogn),因此也称为非线性时间比较类排......
  • 23. Merge k Sorted Lists[Hard]
    23.MergekSortedListsYouaregivenanarrayofklinked-listslists,eachlinked-listissortedinascendingorder.Mergeallthelinked-listsintoonesor......
  • 23/1/31-LeetCode 21: Merge Two Sorted Lists
    MergeTwoSortedLists思路bug注意,一开始我写的是ListNode*ans,*cur;if(list1->val<=list2->val){ ans=cur=list1; list1=list1->next;}else{ ans=......
  • *21. Merge Two Sorted Lists[Easy]
    21.MergeTwoSortedListsYouaregiventheheadsoftwosortedlinkedlistslist1andlist2.Mergethetwolistsinaonesortedlist.Thelistshouldbemad......
  • Pandas的join和merge到底哪个快
    大家好,我是小小明。上次我们的云朵君同学在不严谨的测试下,得出了join可以比merge快5倍的结论。虽然默认参数用法下,join确实比merge快一些,但实际上join并不见得会比merge快。......
  • gitlab Merge Requests操作流程
    引用https://juejin.cn/post/6993548883729055780一、配置受保护的分支创建一个分支例如prd将prd分支也设置为protect分支配置保护分支二、提交者MergeRequests......
  • 导入lodash的深拷贝函数
    不喜欢在一个页面中直接导入一整个lodashimportcloneDeepfrom"lodash/cloneDeep";就可以使用了`差距在哪?以上。......