Created
February 4, 2022 14:04
-
-
Save alex-boom/09df351a23f2897446d9106612b34c2c to your computer and use it in GitHub Desktop.
clone object 4 methods
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 4 способа копирования объектов: | |
const food = { body: { a: 10 }, beef: '3', bacon: '4', say() { console.log('hi') } } | |
// "Spread" не глубокая копия c методами | |
{ ...food } | |
// RESULT: | |
// { body: {a: 70}, beef: '3', bacon: '4', say() { console.log('hi') } } | |
// "Object.assign" не глубокая копия с методами | |
Object.assign({}, food) | |
// RESULT: | |
// { body: {a: 70}, beef: '3', bacon: '4', say() { console.log('hi') } } | |
// "JSON" глубокая копия свойств но без методов | |
JSON.parse(JSON.stringify(food)) | |
// RESULT: | |
// { body: { a: 10 }, beef: '3', bacon: '4' } | |
// "LODASH" глубокая копия всех свойств и методов!!! | |
import _ from 'lodash' | |
_.cloneDeep(food); | |
// RESULT: | |
// { body: { a: 10 }, beef: '3', bacon: '4', say() { console.log('hi') } } | |
food.body.a = 70; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment