الانتقال إلى المحتوى
FrontendJavaScript· 8 دقائق للقراءة

ورا الـ class في JavaScript لسه فيه prototypes شغالة

الـ methods موجودة مرة واحدة على الـ prototype، والـ extends بيربط objects ببعض، والـ fields بس هي اللي بتاعة كل instance.

ReadIn

مقال كل يوم يفرق معاك

21 نوفمبر 2026
  • كاتب موثّق
  • ES2025
محتويات المقال

تيم بيضيف SubmitButton extends Button وبيعمل override لـ handleClick عشان تنادي super.handleClick() الأول، فالـ tracking بتاع الكليك اللي في الـ base class يفضل شغال. في Java أو C#، ده override من الكتاب بالظبط. لكن هنا، أول كليك بيرمي، في Chrome زي Node، TypeError: (intermediate value).handleClick is not a function. والـ base class واضح إن فيه handleClick؛ موجودة قدامك في الكود. المشكلة في الطريقة اللي اتكتبت بيها: arrow function متحطوطة في field.

الفكرة الغلط الشائعة

الفكرة بتقول: الـ classes في JavaScript شغالة زي الـ classes في Java أو C#. كل instance شايل نسخته الخاصة من الـ methods بتاعة الـ class، والـ extends بتنسخ الـ members بتوع الأب جوه الابن.

وسهل تفهم ليه الناس بتصدّق كده. الـ syntax متصمّم عشان يبان مألوف: class وconstructor وextends وsuper، والـ TypeScript بتزوّد فوقهم access modifiers و interfaces. ومن بره، الـ instance شكله مالك كل حاجة: account.deposit() شغالة، و"deposit" in account قيمتها true. وفيه pattern مشهور، الـ arrow-function field، بيدّي فعلًا كل instance نسخته الخاصة، وده بيأكد الصورة أكتر. ونص الحقيقة ده بيخبّي فين الحاجات عايشة فعلًا.

اللي بيحصل فعلًا

الـ class هي constructor function ومعاها prototype object

الـ MDN بتسمي الـ classes "special functions" مبنية على الـ prototypes، وليها شوية syntax وقواعد خاصة بيها. والـ typeof Account قيمتها "function". ولما تنادي new Account()، JavaScript بتعمل object الـ [[Prototype]] الداخلي بتاعه بيشاور على Account.prototype، وبعدين تشغّل الـ constructor عليه. وكل الـ instances بيشاركوا الـ prototype object ده نفسه.

الـ methods موجودة مرة واحدة، على الـ prototype

الـ reference بتاع الـ classes في MDN بيقولها مباشرةً: الـ methods بتتعرّف على الـ prototype وكل الـ instances بيشاركوها. أما الـ fields فحاجة تانية: الـ public instance fields موجودة على كل instance بيتعمل، كـ properties بتاعته هو. وقراية أي property بتمشي في الـ : JavaScript بتبص في الـ object نفسه، وبعدين في الـ prototype بتاعه، وبعدين في الـ prototype بتاع ده، لحد ما تلاقي الاسم أو توصل للآخر.

JavaScript account.js
class Account {  balance = 0; // field: an own property of each instance  deposit(amount) { // method: one function on Account.prototype    this.balance += amount;  }}class SavingsAccount extends Account {}const a = new Account();const s = new SavingsAccount();console.log(typeof Account); // functionconsole.log(Object.hasOwn(a, "balance")); // trueconsole.log(Object.hasOwn(a, "deposit")); // falseconsole.log(a.deposit === s.deposit); // true: one shared functionconst parent = Object.getPrototypeOf(SavingsAccount.prototype);console.log(parent === Account.prototype); // trueAccount.prototype.close = function () { return "closed"; }; // added laterconsole.log(s.close()); // closed: found through the chain at call time

الـ comments دي هي اللي Node 24 و Chromium بيطبعوها. الـ extends ماعملتش نسخة من deposit جوه SavingsAccount؛ هي خلّت الـ prototype بتاع SavingsAccount.prototype يبقى Account.prototype. وعشان كده أي method تتضاف على Account.prototype بعد ما s اتعمل بتشتغل على s عادي: الـ lookup بيحصل وقت النداء، فمفيش أي حاجة اتنسخت عشان تبقى قديمة.

ليه الزرار باظ؟

الـ اللي في field هي field زي أي field تاني: property خاصة بالـ instance، بتتعمل من جديد لكل instance، وعمرها ما بتتحط على الـ prototype. والـ MDN بتشرح التوقيت: الـ base class بتضيف الـ fields بتاعتها قبل ما جسم الـ constructor يشتغل، والـ subclass بتضيف الـ fields بتاعتها بعد ما super() يرجع على طول. يعني Button بتحط الـ arrow بتاعتها على الـ object الجديد الأول، وبعدين SubmitButton بتعرّف arrow تانية بنفس الاسم وبتشيل الأولانية.

وجوه الـ field initializer، الـ super بيشاور على الـ prototype بتاع الـ base class، واللي الـ MDN بتوضح إن فيه الـ instance methods بتاعة الـ base class بس، مش الـ instance fields. والـ Button.prototype مفيهوش handleClick، فالـ super.handleClick قيمتها undefined، ونداءها بيرمي error.

JavaScript buttons.js
class Button {  handleClick = () => console.log("Button: track click");}class SubmitButton extends Button {  handleClick = () => {    super.handleClick();    console.log("SubmitButton: submit form");  };}new SubmitButton().handleClick();// TypeError: (intermediate value).handleClick is not a function

مع الـ methods، النسختين بتوع handleClick موجودين على الـ prototypes بتوعهم، والـ super.handleClick() بتلاقي اللي على Button.prototype. الـ arrow fields كانت موجودة عشان تحافظ على this لما الـ handler يتبعت لحد تاني؛ وwrapper صغير في المكان اللي بتسجّل فيه الـ listener بيعمل نفس الشغلانة، زي ما مقال الـ this اللي قبل كده في نفس المسار بيوضّح.

ومش مجرد sugar برضه

الـ classes لسه functions و prototypes، بس ليها قواعد زيادة. جسم الـ class دايمًا بيشتغل في الـ . ولو ناديت class من غير new بترمي TypeError: Class constructor Account cannot be invoked without 'new'، ومينفعش تستخدم الـ class declaration قبل السطر اللي بيعرّفها. والـ private fields زي #balance، زي ما الـ MDN بتقول، مالهاش مقابل بسيط في الـ constructor functions.

الفكرة الغلطالحقيقة
كل instance بياخد نسخة من كل methodالـ methods موجودة مرة واحدة على Class.prototype، والـ instances بتوصلها عن طريق الـ chain
الـ extends بتنسخ members الأب جوه الابنالـ extends بتربط Child.prototype بـ Parent.prototype، والـ lookup بيمشي في الرابط ده
الـ class نوع جديد مختلف عن الـ functionsالـ typeof لأي class بيدّي "function"، والـ new بيبني object مربوط بالـ prototype بتاعها
الـ arrow-function fields هي methods بـ this ثابتدي properties خاصة بالـ instance: function لكل instance، ومفيش حاجة الـ super يلاقيها

تطبقها إزاي في مشروعك

  1. الـ methods للسلوك والـ fields للداتاحط السلوك في methods، عشان يبقى متشارك وتقدر تعمله override بـ super. وسيب الـ fields للـ state الخاصة بكل instance.
  2. الـ arrow fields للـ handlers اللي محدش هيعملها overrideلو الـ class معمولة عشان حد يعمل منها extend، متكتبش السلوك اللي ممكن يتعمله override كـ arrow fields. اعمل wrap للنداء في المكان اللي بتسجّل فيه الـ listener، أو اعمل bind في الـ constructor، وسيب الـ method نفسها على الـ prototype.
  3. اعمل debug وانت فاكر الـ chainلما property تبان "مش موجودة" أو "غلط"، شيّك بـ Object.hasOwn وامشي في Object.getPrototypeOf عشان تشوف أنهي object هو اللي بيرد على الـ lookup فعلًا.
  4. سيب الـ prototypes بتاعة الـ built-ins في حالهاعمرك ما تضيف methods على Array.prototype أو Object.prototype أو أي built-in تاني. اكتب helper function أو subclass بدل كده.

الخلاصة

  • الـ class في JavaScript هي constructor function ومعاها prototype object؛ والـ typeof لأي class بيدّي "function".
  • الـ methods متخزّنة مرة واحدة على الـ prototype ومتشاركة؛ والـ instances بتلاقيها وهي ماشية في الـ prototype chain.
  • الـ extends بتربط الـ prototypes بدل ما تنسخ الـ members، فأي تغيير في prototype الأب بيبان في كل instance من الـ subclass.
  • الـ fields، ومعاها الـ arrow-function fields، properties خاصة بتتعمل لكل instance، والـ super مايقدرش يوصلها.
  • الـ classes لسه بتضيف قواعد حقيقية (الـ strict mode، والـ new الإجباري، والـ private fields) فوق الـ prototypes.

📚 المصادر

  1. Inheritance and the prototype chain — MDN
  2. Classes — MDN
  3. Public class fields — MDN
  4. Using classes — MDN
  5. Object.getPrototypeOf() — MDN

التعليقات (0)