super
super truy cập và gọi function trên parent object. super.prop và super[expr] hợp lệ trong method definition (class và object literal). Dùng trong class extended (dùng extends).
Cú pháp
super(arguments);
Ví dụ
Class Person extend sang Student. super truy cập property/method cha, this truy cập property con.
<html> <body> <script> class Person { constructor(name, grade) { this.name = name; this.grade = grade; } goal() { return `${this.name} wants to become a cricketer!`; } interest() { return `${this.name} interested in cricket!`; } } class Student extends Person { constructor(name, grade) { super(name, grade); } need() { return `${this.name} needs a cricket kit`; } career() { return `${super.interest()} ${super.goal()} ${this.need()}`; } } const student = new Student('Rishab pant', '7'); document.write(student.career()); </script> </body> </html>
Kết quả
Rishab pant interested in cricket! Rishab pant wants to become a cricketer! Rishab pant needs a cricket kit
Quy tắc:
- Constructor con:
super()bắt buộc trướcthis super.method()gọi method cha (có thể override)super.proptruy cập property cha- Trong object literal:
supertrỏ đến prototype