0% found this document useful (0 votes)
4 views

Javascript 2

Uploaded by

jtomm2covj
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Javascript 2

Uploaded by

jtomm2covj
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

or by using the spread operator

var hobbies = ['Sports', 'Cooking']


var copiedHobbies = [...hobbies]

for Objects
var person = { name: 'Max' }
var copiedPerson = Object.assign({}, person)

var person = { name: 'Max' }


var copiedPerson = { ...person }

let student1 = {
name: "Manish",
company: "Gfg"

}
let student2 = JSON.parse(JSON.stringify(student1))

Here's one super-important thing to note though: You're not creating deep clones
with either approach!
If you cloned array contains nested arrays or objects as elements or if your
object contains properties that hold arrays or other objects, then these nested
arrays and objects will not have been cloned!
You still have the old pointers, pointing to the old nested arrays/ objects!
You'd have to manually clone every layer that you plan on working with. If you
don't plan on changing these nested arrays or objects though, you don't need to
clone them.

Here's one super-important thing to note though: You're not creating deep clones
with either approach!
If you cloned array contains nested arrays or objects as elements or if your
object contains properties that hold arrays or other objects, then these nested
arrays and objects will not have been cloned!
You still have the old pointers, pointing to the old nested arrays/ objects!
You'd have to manually clone every layer that you plan on working with. If you
don't plan on changing these nested arrays or objects though, you don't need to
clone them.
https://redux.js.org/usage/structuring-reducers/immutable-update-
patterns#immutable-update-patterns

You might also like