JavaScript-এ OOP-তে কাল্পনিক বৈশিষ্ট্যগুলির সেটার
কাল্পনিক বৈশিষ্ট্যের গেটার ছাড়াও সেগুলির সেটারও তৈরি করা যায়। এই ক্ষেত্রে, সেটারের ভিতরে আমাদের উচিত ডেটা পাওয়া, সেগুলিকে বিভক্ত করা এবং সংশ্লিষ্ট অংশগুলি প্রয়োজনীয় পাবলিক বৈশিষ্ট্যগুলিতে লিখা।
চেষ্টা করি। আসুন কাল্পনিক বৈশিষ্ট্য full-এর সেটার বাস্তবায়ন করি:
javascriptclass User {
constructor(name, surn) {
this.name = name;
this.surn = surn;
}
get full() {
return this.name + ' ' + this.surn;
}
set full(full) {
let [name, surn] = full.split(' ');
this.name = name;
this.surn = surn;
}
}
ডেস্ট্রাকচারিং সহজ করা যাক:
javascriptclass User {
constructor(name, surn) {
this.name = name;
this.surn = surn;
}
get full() {
return this.name + ' ' + this.surn;
}
set full(full) {
[this.name, this.surn] = full.split(' ');
}
}
ক্লাসের একটি অবজেক্ট তৈরি করা যাক:
javascriptlet user = new User('john', 'smit');
আমাদের কাল্পনিক সেটারে ডেটা লিখি:
javascriptuser.full = 'eric jons';
যাচাই করা যাক যে অবজেক্টের বৈশিষ্ট্যগুলি পরিবর্তন হয়েছে:
javascriptconsole.log(user.name); // 'eric'
console.log(user.surn); // 'jons'
console.log(user.full); // 'eric jons'