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

التغليف هو عملية إخفاء تفاصيل التنفيذ الداخلية للكائن عن العالم الخارجي. يمكن تحقيق ذلك في JavaScript باستخدام الفئات والفئات الخاصة (private classes).

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