--- title: super slug: Web/JavaScript/Reference/Operators/super translation_of: Web/JavaScript/Reference/Operators/super ---
Từ khóa super được sử dụng để gọi các hàm trên đối tượng cha.
Các biểu thức super.prop và super[expr] là hợp lệ trong mọi định nghĩa phương thức ở cả classes và object literals.
super([arguments]); // gọi hàm khởi tạo cha. super.functionOnParent([arguments]);
Khi được sử dụng trong một hàm khởi tạo, từ khóa super xuất hiện một mình và phải được sử dụng trước khi từ khóa this có thể sử dụng. Từ khóa này cũng có thể được sử dụng để gọi các hàm trên đối tượng cha.
Đoạn code này lấy từ ví dụ về class (live demo). super()
ở đây được gọi để tránh việc lặp lại các phần giống nhau của Rectangle
và Square
.
class Polygon { constructor(height, width) { this.name = 'Polygon'; this.height = height; this.width = width; } sayName() { console.log('Hi, I am a ', this.name + '.'); } } class Square extends Polygon { constructor(length) { this.height; // ReferenceError, super needs to be called first! // Here, it calls the parent class' constructor with lengths // provided for the Polygon's width and height super(length, length); // Note: In derived classes, super() must be called before you // can use 'this'. Leaving this out will cause a reference error. this.name = 'Square'; } get area() { return this.height * this.width; } set area(value) { this.area = value; } }
Bạn cũng có thể gọi super()
trên các phương thức tĩnh.
class Human { constructor() {} static ping() { return 'ping'; } } class Computer extends Human { constructor() {} static pingpong() { return super.ping() + ' pong'; } } Computer.pingpong(); // 'ping pong'
Bạn không thể sử dụng thao tác delete và super.prop
hoặc super[expr]
để xóa một thuộc tính của lớp cha, nó sẽ ném lỗi {{jsxref("ReferenceError")}}.
class Base { constructor() {} foo() {} } class Derived extends Base { constructor() {} delete() { delete super.foo; } } new Derived().delete(); // ReferenceError: invalid delete involving 'super'.
Khi định nghĩa các thuộc tính non-writable ví dụ {{jsxref("Object.defineProperty")}}, super
không thể ghi đè giá trị của thuộc tính này.
class X { constructor() { Object.defineProperty(this, "prop", { configurable: true, writable: false, value: 1 }); } f() { super.prop = 2; } } var x = new X(); x.f(); console.log(x.prop); // 1
Super cũng có thể được sử dụng trong khởi tạo đối tượng hoặc literal. Trong ví dụ này, 2 đối tượng định nghĩa một phương thức. Trong đối tượng thứ hai, super
gọi phương thức của đối tượng thứ nhất. Điều này làm được với sự trợ giúp của {{jsxref("Object.setPrototypeOf()")}} cái giúp chúng ta có thể thiết lập prototype của obj2
thành obj1
, vì thế super
có thể tìm method1
trên obj1
.
var obj1 = { method1() { console.log("method 1"); } } var obj2 = { method2() { super.method1(); } } Object.setPrototypeOf(obj2, obj1); obj2.method2(); // logs "method 1"
Specification | Status | Comment |
---|---|---|
{{SpecName('ES6', '#sec-super-keyword', 'super')}} | {{Spec2('ES6')}} | Initial definition. |
{{SpecName('ESDraft', '#sec-super-keyword', 'super')}} | {{Spec2('ESDraft')}} |
{{CompatibilityTable}}
Tính năng | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Hỗ trợ cơ bản | {{CompatChrome(42.0)}} | {{CompatGeckoDesktop(45)}} | {{CompatUnknown}} | {{CompatUnknown}} | {{CompatUnknown}} |
Tính năng | Android | Android Webview | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile | Chrome for Android |
---|---|---|---|---|---|---|---|
Hỗ trợ cơ bản | {{CompatUnknown}} | {{CompatChrome(42.0)}} | {{CompatGeckoMobile(45)}} | {{CompatUnknown}} | {{CompatUnknown}} | {{CompatUnknown}} | {{CompatChrome(42.0)}} |