_.extend vs _.clone in lodash

To inherit the properties of one object from another when working with JavaScript objects, I often see a use _.clonewhere there was an intention to create an object with other properties of the object, and then it will be expanded.

Why can't we use _.extend, which is more important for expanding an object?

Please tell me the difference between the two and why we cannot _.extendinstead _.clone, which is an expensive operation.

+4
source share
2 answers

_.extend . _.clone , . , _.extend _.assignIn.

_.assignIn(object, [sources])

. object.

https://lodash.com/docs/4.17.2#assignIn

. _.clone:

https://lodash.com/docs/4.17.2#clone

+3

_.extend() , . _.clone(), .

, , :

_.extend({}, originalObject, {
    additionalProperty: "foo"
})

originalObject. ES2015 JavaScript Object.assign().


:

+2

Source: https://habr.com/ru/post/1663337/


All Articles