شرح صفوف ومحتوى الفقرة
متى عرّف الصنف الابن دالة constructor(...) خاصة به، وجب عليه استدعاء super(...) قبل أي قراءة أو كتابة على الكائن this. فالباني المشتق في JavaScript لا يحجز كائن النسخة الفارغ بنفسه، بل يفوض إنشاء النسخة وتهيئة حقول الأب إلى باني الصنف الأب عبر super(...).
أي محاولة لكتابة this.tier = tier قبل سطر super(owner) تطلق فوراً الخطأ الصريح ReferenceError.
JavaScript
class Account {
constructor(owner) { this.owner = owner; }
badge() { return `User: ${this.owner}`; }
}
class ProAccount extends Account {
constructor(owner, tier) { super(owner); this.tier = tier; }
}
const pro = new ProAccount('Nour', 'Gold');
document.getElementById('inh-out').textContent =
`${pro.badge()} | Tier: ${pro.tier}`;