DeveloperBreeze

تعدد الأشكال Development Tutorials, Guides & Insights

Unlock 1+ expert-curated تعدد الأشكال tutorials, real-world code snippets, and modern dev strategies. From fundamentals to advanced topics, boost your تعدد الأشكال skills on DeveloperBreeze.

البرمجة الكائنية (OOP) في JavaScript: المفاهيم الأساسية والتطبيقات

Tutorial September 26, 2024
javascript

class BankAccount {
    constructor(owner, balance) {
        this.owner = owner;
        this._balance = balance;  // خاص
    }

    deposit(amount) {
        if (amount > 0) {
            this._balance += amount;
            console.log(`تم إضافة ${amount} إلى الحساب.`);
        }
    }

    getBalance() {
        return this._balance;
    }
}

const account = new BankAccount("علي", 1000);
account.deposit(500);
console.log(account.getBalance());  // 1500

في هذا المثال، خاصية _balance مخفية ولا يمكن الوصول إليها مباشرة من خارج الفئة.