Šajā piemērā jūs iemācīsities rakstīt programmu, kas klonē objektu.
Lai saprastu šo piemēru, jums jāpārzina šādas JavaScript programmēšanas tēmas:
- JavaScript objekti
- JavaScript Object.assign ()
JavaScript objekts ir sarežģīts datu tips, kas var saturēt dažādus datu tipus. Piemēram,
const person = ( name: 'John', age: 21, )
Šeit person
ir objekts. Tagad, veicot kaut ko līdzīgu, jūs nevarat klonēt objektu.
const copy = person; console.log(copy); // (name: "John", age: 21)
Iepriekš minētajā programmā copy
mainīgajam ir tāda pati vērtība kā person
objektam. Tomēr, ja maināt copy
objekta vērtību, person
mainīsies arī objekta vērtība . Piemēram,
copy.name = 'Peter'; console.log(copy.name); // Peter console.log(person.name); // Peter
Izmaiņas ir redzamas abos objektos, jo objekti ir atsauces tipi . Un abi copy
un person
norāda uz vienu un to pašu objektu.
1. piemērs. Klonējiet objektu, izmantojot Object.assign ()
// program to clone the object // declaring object const person = ( name: 'John', age: 21, ) // cloning the object const clonePerson = Object.assign((), person); console.log(clonePerson); // changing the value of clonePerson clonePerson.name = 'Peter'; console.log(clonePerson.name); console.log(person.name);
Rezultāts
(vārds: "Jānis", vecums: 21) Pēteris Džons
Object.assign()
Metode ir daļa no ES6 standartu. Par Object.assign()
metode Veic dziļi kopēt un kopē visas īpašības no viena vai vairākiem objektiem.
Piezīme : tukšais ()
kā pirmais arguments nodrošina, ka nemaināt sākotnējo objektu.
2. piemērs: Objekta klonēšana, izmantojot izplatīšanas sintaksi
// program to clone the object // declaring object const person = ( name: 'John', age: 21, ) // cloning the object const clonePerson = (… person) console.log(clonePerson); // changing the value of clonePerson clonePerson.name = 'Peter'; console.log(clonePerson.name); console.log(person.name);
Rezultāts
(vārds: "Jānis", vecums: 21) Pēteris Džons
Izplatītās sintakse …
tika ieviesta jaunākajā versijā (ES6).
Izkliedēto sintaksi var izmantot, lai izveidotu seklu objekta kopiju. Tas nozīmē, ka tas kopēs objektu. Tomēr atsaucas uz dziļākiem objektiem. Piemēram,
const person = ( name: 'John', age: 21, // the inner objects will change in the shallow copy marks: ( math: 66, english: 73) ) // cloning the object const clonePerson = (… person) console.log(clonePerson); // (name: "John", age: 21, marks: (… )) // changing the value of clonePerson clonePerson.marks.math = 100; console.log(clonePerson.marks.math); // 100 console.log(person.marks.math); // 100
Lūk, kad iekšējais objekts vērtība math
tiek mainīts uz 100 no clonePerson
objekta, vērtība math
atslēgā person
objekta arī mainās.
3. piemērs: Objekta klonēšana, izmantojot JSON.parse ()
// program to clone the object // declaring object const person = ( name: 'John', age: 21, ) // cloning the object const clonePerson = JSON.parse(JSON.stringify(person)); console.log(clonePerson); // changing the value of clonePerson clonePerson.name = 'Peter'; console.log(clonePerson.name); console.log(person.name);
Rezultāts
(vārds: "Jānis", vecums: 21) Pēteris Džons
Iepriekš minētajā programmā JSON.parse()
metodi izmanto objekta klonēšanai.
Piezīme : JSON.parse()
darbojas tikai ar Number
un String
burtiski objektu. Tas nedarbojas ar objektu burtiski ar function
vai symbol
īpašībām.