البرمجة كائنية التوجه في JavaScript 🎭
ES6 قدّم بناء Class الذي يسهل كتابة OOP.
ES6 قدّم بناء Class الذي يسهل كتابة OOP.
// اضغط ▶ تشغيل لرؤية النتائج...
أنشئ Class يمثل حساب بنكي مع إيداع، سحب، تاريخ العمليات.
class BankAccount {
#balance = 0;
#history = [];
constructor(owner, initial = 0) {
this.owner = owner;
this.#balance = initial;
this.#log("فتح حساب", initial);
}
#log(type, amount) {
this.#history.push({ type, amount, date: new Date().toLocaleDateString("ar") });
}
deposit(amount) {
this.#balance += amount;
this.#log("إيداع", amount);
console.log(`✅ إيداع ${amount}. الرصيد: ${this.#balance}`);
}
withdraw(amount) {
if (amount > this.#balance) {
console.log("❌ رصيد غير كافٍ");
return;
}
this.#balance -= amount;
this.#log("سحب", amount);
console.log(`✅ سحب ${amount}. الرصيد: ${this.#balance}`);
}
get balance() { return this.#balance; }
get history() { return this.#history; }
}
const acc = new BankAccount("أحمد", 1000);
acc.deposit(500);
acc.withdraw(200);
console.log("الرصيد:", acc.balance);
console.log("السجل:", acc.history);