
ورا الـ class في JavaScript لسه فيه prototypes شغالة
- كاتب موثّق
- 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.
الفكرة الغلط الشائعة
extends بتنسخ الـ members بتوع الأب جوه الابن.
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
typeof Account قيمتها "function". ولما تنادي new Account()، JavaScript بتعمل object الـ [[Prototype]] الداخلي بتاعه بيشاور على Account.prototype، وبعدين تشغّل الـ constructor عليه. وكل الـ instances بيشاركوا الـ prototype object ده نفسه.
الـ methods موجودة مرة واحدة، على الـ prototype
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 timeextends ماعملتش نسخة من deposit جوه SavingsAccount؛ هي خلّت الـ prototype بتاع SavingsAccount.prototype يبقى Account.prototype. وعشان كده أي method تتضاف على Account.prototype بعد ما s اتعمل بتشتغل على s عادي: الـ lookup بيحصل وقت النداء، فمفيش أي حاجة اتنسخت عشان تبقى قديمة.
ليه الزرار باظ؟
super() يرجع على طول. يعني Button بتحط الـ arrow بتاعتها على الـ object الجديد الأول، وبعدين SubmitButton بتعرّف arrow تانية بنفس الاسم وبتشيل الأولانية.
super بيشاور على الـ prototype بتاع الـ base class، واللي الـ MDN بتوضح إن فيه الـ instance methods بتاعة الـ base class بس، مش الـ instance fields. والـ Button.prototype مفيهوش handleClick، فالـ super.handleClick قيمتها undefined، ونداءها بيرمي error.
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 functionclass Button { handleClick() { console.log("Button: track click"); }}class SubmitButton extends Button { handleClick() { super.handleClick(); console.log("SubmitButton: submit form"); }}const button = new SubmitButton();const onClick = () => button.handleClick(); // keeps `this` for a listeneronClick();// Button: track click// SubmitButton: submit formhandleClick موجودين على الـ prototypes بتوعهم، والـ super.handleClick() بتلاقي اللي على Button.prototype. الـ arrow fields كانت موجودة عشان تحافظ على this لما الـ handler يتبعت لحد تاني؛ وwrapper صغير في المكان اللي بتسجّل فيه الـ listener بيعمل نفس الشغلانة، زي ما مقال الـ this اللي قبل كده في نفس المسار بيوضّح.
ومش مجرد sugar برضه
new بترمي TypeError: Class constructor Account cannot be invoked without 'new'، ومينفعش تستخدم الـ class declaration قبل السطر اللي بيعرّفها. والـ private fields زي #balance، زي ما الـ MDN بتقول، مالهاش مقابل بسيط في الـ constructor functions.
Class.prototype، والـ instances بتوصلها عن طريق الـ chain | |
extends بتنسخ members الأب جوه الابن | extends بتربط Child.prototype بـ Parent.prototype، والـ lookup بيمشي في الرابط ده |
typeof لأي class بيدّي "function"، والـ new بيبني object مربوط بالـ prototype بتاعها | |
this ثابت | super يلاقيها |
تطبقها إزاي في مشروعك
الـ methods للسلوك والـ fields للداتا حط السلوك في methods، عشان يبقى متشارك وتقدر تعمله override بـ super. وسيب الـ fields للـ state الخاصة بكل instance.الـ arrow fields للـ handlers اللي محدش هيعملها override لو الـ class معمولة عشان حد يعمل منها extend، متكتبش السلوك اللي ممكن يتعمله override كـ arrow fields. اعمل wrap للنداء في المكان اللي بتسجّل فيه الـ listener، أو اعمل bind في الـ constructor، وسيب الـ method نفسها على الـ prototype. اعمل debug وانت فاكر الـ chain لما property تبان "مش موجودة" أو "غلط"، شيّك بـ Object.hasOwnوامشي فيObject.getPrototypeOfعشان تشوف أنهي object هو اللي بيرد على الـ lookup فعلًا.سيب الـ 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.
التعليقات (0)