Bu örnekte, bir nesneyi klonlayan bir program yazmayı öğreneceksiniz.
Bu örneği anlamak için, aşağıdaki JavaScript programlama konuları hakkında bilgi sahibi olmalısınız:
- JavaScript Nesneleri
- JavaScript Object.assign ()
JavaScript nesnesi, çeşitli veri türlerini içerebilen karmaşık bir veri türüdür. Örneğin,
const person = ( name: 'John', age: 21, )
İşte person
bir nesne. Şimdi, böyle bir şey yaparak bir nesneyi klonlayamazsınız.
const copy = person; console.log(copy); // (name: "John", age: 21)
Yukarıdaki programda, copy
değişken person
nesne ile aynı değere sahiptir . Ancak copy
nesnenin değerini değiştirirseniz, nesnedeki değer person
de değişecektir. Örneğin,
copy.name = 'Peter'; console.log(copy.name); // Peter console.log(person.name); // Peter
Değişiklik her iki nesnede de görülür çünkü nesneler referans türleridir . Ve her ikisi de copy
ve person
aynı nesneye işaret ediyor.
Örnek 1. Object.assign () öğesini kullanarak Nesneyi Klonlayın
// 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);
Çıktı
(isim: "John", yaş: 21) Peter John
Object.assign()
Yöntem bir parçasıdır ES6 standart. Object.assign()
Yöntem gerçekleştirdiği derin kopyalayıp bir veya birden fazla nesnelerden kopyalar tüm özellikleri.
Not : ()
İlk bağımsız değişken olarak boş , orijinal nesneyi değiştirmemenizi sağlar.
Örnek 2: Yayılmış Sözdizimini Kullanarak Nesneyi Klonlayın
// 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);
Çıktı
(isim: "John", yaş: 21) Peter John
Yayılma sözdizimi …
sonraki sürümde (ES6) tanıtıldı.
Yayılma sözdizimi, bir nesnenin yüzeysel bir kopyasını oluşturmak için kullanılabilir. Bu, nesneyi kopyalayacağı anlamına gelir. Bununla birlikte, daha derin nesnelere başvurulur. Örneğin,
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
Burada iç nesne değeri nesnenin 100'ümath
olarak değiştirildiğinde , nesnenin anahtarının değeri de değişir.clonePerson
math
person
Örnek 3: JSON.parse () Kullanarak Nesneyi Klonlayın
// 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);
Çıktı
(isim: "John", yaş: 21) Peter John
Yukarıdaki programda, JSON.parse()
yöntem bir nesneyi klonlamak için kullanılır.
Not : JSON.parse()
yalnızca Number
ve String
nesne değişmeziyle çalışır . function
Veya symbol
özellikleriyle değişmez bir nesne ile çalışmaz .