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({},
?
1 Answer
Reset to default 4The 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条)