Menu
7.8 জাভাস্ক্রিপ্টে প্রাইভেট প্রোপার্টি হাইড করা
অবজেক্ট ওরিয়েন্টেড প্রোগ্রামিং কনসেপ্টের সবচেয়ে গুরুত্বপূর্ণ টপিকগুলোর একটা হলো – internal ইন্টারফেস থেকে external ইন্টারফেসকে আলাদা করা। মানে কোডের কঠিন ইমপ্লিমেন্টেশন লজিকগুলোকে আলাদা করা এবং ইউজারকে শুধুমাত্র বাইরের ফাংশনালিটি দেখানো। যেকোনো সফটওয়্যার তৈরির ক্ষেত্রে আমাদের এই কনসেপ্ট জানা দরকার। চলুন একটা বাস্তব উদাহরণ দিয়ে বোঝা যাক। আমরা কফি মেশিনের সাথে সবাই পরিচিত। কফি বানানোর জন্য আমাদের সাধারণত কি করতে হয়? উপরে দিয়ে কফি, দুধ আর চিনি দিয়ে কয়েকটা বাটনে টিপ দিতে হয়, খুবই সহজ তাইনা?

class CoffeeMachine {
waterAmount = 0;
sugarAmount = 0;
constructor(power) {
this.power = power;
console.log(“Created a coffee machine with power: “+power)
}
}
// কফি মেশিন থেকে নতুন কফি অবজেক্ট তৈরি
let coffee = new CoffeeMachine(100);
// কফিতে পানির পরিমাণ
coffee.waterAmount = 200;
class CoffeeMachine {
waterAmount = 0;
_sugarAmount = 0;
set sugarAmount(value) {
if (value < 0) {
value = 0;
}
this._sugarAmount = value;
}
get sugarAmount(){
return this._sugarAmount;
}
constructor(power) {
this.power = power;
console.log(“Created a coffee machine with power: “+power)
}
}
// কফি মেশিন থেকে নতুন কফি অবজেক্ট তৈরি
let coffee = new CoffeeMachine(100);
// কফিতে চিনির পরিমাণ
coffee.sugarAmount = -100;
console.log(coffee.sugarAmount) // আউটপুটঃ 0
class CoffeeMachine {
#waterAmount = 0;
set setWaterAmount(value) {
this.#waterAmount = value;
}
}
let coffee = new CoffeeMachine();
coffee.setWaterAmount = 100;
coffee.#waterAmount; // SyntaxError: Private field ‘#waterAmount’ must be declared in an enclosing class
class CoffeeMachine {
#waterAmount = 0;
set waterAmount(value) {
this.#waterAmount = value;
}
get waterAmount() {
return this.#waterAmount;
}
}
let coffee = new CoffeeMachine();
coffee.waterAmount = 10;
console.log(coffee.waterAmount);
উদাহরণ-১:
class Person {#address;#mobile;constructor(name) {this.name = name;}setAddress(address) {this.#address = address;}setMobile(mobileNumber) {this.#mobile = mobileNumber;}getDetails() {console.log(`${this.name} from ${this.#address}, Mobile: ${this.#mobile}`);}}const teaBoy = new Person(‘Foyez’);console.log(teaBoy); // Person { name: ‘Foyez’ }teaBoy.setAddress(‘Sylhet’);teaBoy.setMobile(‘019502080’);teaBoy.getDetails(); // Foyez from Sylhet, Mobile: 019502080উদাহরণ-২:
class Student {#grade;constructor(name, roll) {this.name = name;this.roll = roll;}setGrade(grade) {this.#grade = grade;}getGrade() {return this.#grade;}}const rahim = new Student(‘Rahim’, 101);console.log(rahim); // Student { name: ‘Rahim’, roll: 101 }rahim.setGrade(‘A+’);console.log(rahim.getGrade()); // A+উদাহরণ-৩:
class Fund {#currentBalance = 0;set currentBalance(amount) {this.#currentBalance = amount;}get currentBalance() {return this.#currentBalance;}}const orphanageFund = new Fund();orphanageFund.currentBalance = 780450;console.log(‘Current balance: ‘, orphanageFund.currentBalance); // Current balance: 780450উদাহরণ-৪:
class Account {#balance = 0;constructor(initBalance) {this.#balance = initBalance;}deposite(amount) {this.#balance = this.#balance + amount;console.log(‘Current balance: ‘, this.#balance);}withdraw(amount) {this.#balance = this.#balance – amount;console.log(‘Current balance: ‘, this.#balance);}}const myBankAccount = new Account(14780920);myBankAccount.deposite(70500); // Current balance: 14851420myBankAccount.withdraw(2280500); // Current balance: 12570920
- একটি ক্লাসের প্রাইভেট প্রোপার্টি ডিফাইন করো এবং get/set মেথডের মাধ্যমে সেটার মানের পরিবর্তন করো।
- Friends নামে একটি ক্লাস তৈরি করো যেটার মধ্যে পাবলিক ফ্রেন্ড লিস্ট ও প্রাইভেট ফ্রেন্ড লিস্ট থাকবে এবং ঐ লিস্টগুলোতে ফ্রেন্ড যোগ করার মেথড তৈরি করো।
- এমন একটি ক্লাস তৈরি করো যার প্রাইভেট প্রোপার্টি দেখানো হবে কোন নির্দিষ্ট শর্ত সাপেক্ষে।
- ক্লাসের মধ্যে একটি প্রাইভেট মেথড ডিক্লেয়ার করো যেটার কার্য সম্পাদন করা হবে অন্য কোন পাবলিক মেথড দ্বারা।
- BkashAccount নামে একটি ক্লাস তৈরি করো যার একটি মেথডের মাধ্যমে প্রাইভেট ব্যালেন্স থেকে টাকা ট্রান্সফার করা যাবে।