javascript - What is the difference between cloneDeep(a) and extend({}, a) in lodash? - Stack Overflow

For a long time I was thinking that_.extend({}, obj) is the same as _.cloneDeep(obj)in lodash.But I fou

For a long time I was thinking that

_.extend({}, obj) is the same as _.cloneDeep(obj)

in lodash.

But I found out that object, created with extend function has the same __proto__ hash, unlike object, created with cloneDeep function.


Please, explain what is the difference between Lodash's cloneDeep and extend({},?

For a long time I was thinking that

_.extend({}, obj) is the same as _.cloneDeep(obj)

in lodash.

But I found out that object, created with extend function has the same __proto__ hash, unlike object, created with cloneDeep function.


Please, explain what is the difference between Lodash's cloneDeep and extend({},?

Share Improve this question asked Feb 12, 2016 at 15:01 James AkwuhJames Akwuh 2,2172 gold badges24 silver badges25 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 4

The key difference is cloneDeep returns a new object while extend mutates the object in place.

var a = {x: 1};
_.extend(a, {}) === a // true
_.cloneDeep(a) === a // false

In your example:

_.extend({}, a) === a // false
_.cloneDeep(a) === a // false

what you are extending is not a, but the empty object {}. So when you strictly pare the result of _.extend({}, a) with a, you are paring an extended empty object. When you are paring _.cloneDeep(a) with a, you are paring a clone of a with itself. Thus, they may give the same result, but the nature is different.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745293799a4621019.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信