الـclass قالب لصنع objects: الـconstructor بيجهّز الحقول، والـmethods بتقعد على prototype، وstatic على الـclassصنفتُنطق كْلَاسقالب بتعمل منه objects ليها نفس الشكل ونفس السلوك. نفسه، و# للحقل الخاص.
What is a class in JavaScript?
المثال
class قالب بيطلّع objects ليها نفس الشكل ونفس السلوك. كل object بيطلع منه اسمه instance. الـconstructor بيشتغل مرة واحدة لما تكتب new وبيجهّز الـinstance.
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; // 150static
الـmethod أو الـproperty اللي قبلها static بتتبع class نفسه مش الـinstance. بتنفع للـfunctions المساعدة اللي مش محتاجة حالة.
class Temp {
static fromF(f) { return new Temp((f - 32) * 5 / 9); }
constructor(c) { this.c = c; }
}
Temp.fromF(212).c; // 100class هي function في الحقيقة
class في جافاسكريبت شكل أنضف لكتابة function constructor و prototype. لو كتبت typeof Account هتاخد 'function'. و class مبيتشالش لفوق زي function: متستخدمهوش قبل ما تعرّفه.
متعملش class لمجرد إنك تحط functions مع بعض. object عادي أو module أبسط. class للحالة اللي محتاجة أكتر من instance بنفس السلوك.
الحالة اللي بتعضّ
methods الـclass بتفقد this لو اتفصلت زي objects بالظبط. ونداء class من غير new TypeError: مش functionدالةتُنطق فَنْكْشِنحتة كود ليها اسم، بتاخد مدخلات وبترجّع ناتج، وبتناديها وقت ما تحب. عادية.
class Counter {
count = 0;
inc() { this.count++; }
}
const c = new Counter();
const { inc } = c;
// inc(); // TypeError — this ضاعت تاني
class A {}
// A(); // TypeError: class لازم newالبرهان
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); }التمرين
اكتب class Counter فيه عدّاد خاص و methods increment وreset وجيتر value.
جرّب بنفسك قبل ما تروح للدرس اللي بعده.
تدريبات متدرّجة
كلاس نقطة
اكتب classصنفتُنطق كْلَاسقالب بتعمل منه objects ليها نفس الشكل ونفس السلوك. Point بconstructorدالة إنشاءتُنطق كُونْسْتْرَكْتُورmethod خاصة بتشتغل مرة واحدة لما تعمل object جديد وبتجهّزه. بياخد x وy ويحفظهم.
class Point {
}
حساب بنكي
اكتب classصنفتُنطق كْلَاسقالب بتعمل منه objects ليها نفس الشكل ونفس السلوك. Account فيه رصيد بيبدأ من 0، وmethodدالة عضوتُنطق مِيثُودfunction عايشة جوه object أو class وبتتنادى منه. deposit(n) بتزوّد الرصيد وترجّعه، وجيتر balance بيرجّع الرصيد.
class Account {
}
وراثة
اكتب classصنفتُنطق كْلَاسقالب بتعمل منه objects ليها نفس الشكل ونفس السلوك. Animal فيه speak() بترجّع '...'، وclassصنفتُنطق كْلَاسقالب بتعمل منه objects ليها نفس الشكل ونفس السلوك. Dog بيرث منه وبيعمل override لـ speak() فترجّع Animal.speak() + 'هاو' باستخدام super.
class Animal {
}
class Dog extends Animal {
}
تحدّي
اتجرّب على Node v26.14.0 · آخر تشغيل