المسارات / JavaScript / الدرس 56

classes

متوسط9 دقايقالدرس 56 من 150تمرين واحد
قضية

الـclass قالب لصنع objects: الـconstructor بيجهّز الحقول، والـmethods بتقعد على prototype، وstatic على الـ نفسه، و# للحقل الخاص.

What is a class in JavaScript?

المصدر على MDN ↗

المثال

class قالب بيطلّع objects ليها نفس الشكل ونفس السلوك. كل object بيطلع منه اسمه instance. الـconstructor بيشتغل مرة واحدة لما تكتب new وبيجهّز الـinstance.

js
class Account {
  owner;                       // property عامة
  #balance = 0;                // خاصة: مبتبانش من بره class

  constructor(owner) {
    this.owner = owner;
  }

  deposit(amount) {
    if (amount <= 0) throw new RangeError('المبلغ يجب أن يكون موجبًا');
    this.#balance += amount;
    return this;
  }

  get balance() { return this.#balance; }
}

const acc = new Account('منى').deposit(100).deposit(50);
acc.balance; // 150

static

الـmethod أو الـproperty اللي قبلها static بتتبع class نفسه مش الـinstance. بتنفع للـfunctions المساعدة اللي مش محتاجة حالة.

js
class Temp {
  static fromF(f) { return new Temp((f - 32) * 5 / 9); }
  constructor(c) { this.c = c; }
}
Temp.fromF(212).c; // 100

class هي function في الحقيقة

class في جافاسكريبت شكل أنضف لكتابة function constructor و prototype. لو كتبت typeof Account هتاخد 'function'. و class مبيتشالش لفوق زي function: متستخدمهوش قبل ما تعرّفه.

قاعدة

متعملش class لمجرد إنك تحط functions مع بعض. object عادي أو module أبسط. class للحالة اللي محتاجة أكتر من instance بنفس السلوك.

الحالة اللي بتعضّ

methods الـclass بتفقد this لو اتفصلت زي objects بالظبط. ونداء class من غير new TypeError: مش عادية.

js
class Counter {
  count = 0;
  inc() { this.count++; }
}
const c = new Counter();
const { inc } = c;
// inc(); // TypeError — this ضاعت تاني

class A {}
// A(); // TypeError: class لازم new

البرهان

شُغّل على Node v26.14.0، والناتج منسوخ من التشغيل نفسه.

js
class Account {
  #balance = 0;
  deposit(n) { this.#balance += n; return this; }
  get balance() { return this.#balance; }
}
const a = new Account().deposit(100);
console.log(a.balance, typeof Account);
try { a.balance = 5; } catch (e) { console.log(e.name); }
try { new Function('return (new (class A { #x })).#x')(); } catch (e) { console.log(e.name); }
النتيجة
100 function
TypeError
SyntaxError
class Account {
  #balance = 0;
  deposit(n) { this.#balance += n; return this; }
  get balance() { return this.#balance; }
}
const a = new Account().deposit(100);
console.log(a.balance, typeof Account);
try { a.balance = 5; } catch (e) { console.log(e.name); }
try { new Function('return (new (class A { #x })).#x')(); } catch (e) { console.log(e.name); }

التمرين

تمرين 01

اكتب class Counter فيه عدّاد خاص و methods increment وreset وجيتر value.

جرّب بنفسك قبل ما تروح للدرس اللي بعده.

تدريبات متدرّجة

من السهل للصعب. بتكتب الحل وتدوس «افحص الحل»، والاختبارات مخفية. بتتحفظ حلولك على جهازك.

سهل

كلاس نقطة

اكتب Point ب بياخد x وy ويحفظهم.

class Point {
  
}
متوسط

حساب بنكي

اكتب Account فيه رصيد بيبدأ من 0، و deposit(n) بتزوّد الرصيد وترجّعه، وجيتر balance بيرجّع الرصيد.

class Account {
  
}
صعب

وراثة

اكتب Animal فيه speak() بترجّع '...'، و Dog بيرث منه وبيعمل override لـ speak() فترجّع Animal.speak() + 'هاو' باستخدام super.

class Animal {
  
}

class Dog extends Animal {
  
}

تحدّي

اختبار على فهم الدرس نفسه. الاختبارات مخفية وبتتغيّر كل مرة، فلو الحل غلط هتشوف مثال بيفضحه.

بنحمّل التحدّي...

∎ تمّ البرهان.

اتجرّب على Node v26.14.0 · آخر تشغيل

ابعت الصفحة لحد
واتسابتليجرامX
برهان

الدروس

اتحلّ 0 من 174 تمرين