Questo contenuto viene visualizzato in inglese perché non è ancora disponibile una versione tradotta nella lingua selezionata. Aiutaci a tradurre questo articolo!
La parola chiave super viene usata per chiamare le funzioni dell'oggetto padre.
Il super.prop ed espressioni con super[expr] sono valide in ogni definizione di metodo sia nelle classi e oggetti literals.
Sintassi
super([arguments]); // chiama il costruttore padre. super.functionOnParent([arguments]);
Descrizione
Quando viene usata in un costruttore, la parola chiave super deve essere usata prima della parola chiave this. Super può essere usata anche per chiamare funzioni dell'oggetto padre.
Esempio
Usare super nelle classi
Questo pezzo di codice è preso da classes sample (live demo).
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 deve essere chiamato per primo!
// Chiama il costruttore della classe padre
super(length, length);
// Nota: Nelle classi derivate super() deve essere chiamato prima
// dell'uso di 'this'.
this.name = 'Square';
}
get area() {
return this.height * this.width;
}
set area(value) {
this.area = value;
}
}
Usare super per metodi statici
Puoi usare super anche chiamare metodi statici.
class Human {
constructor() {}
static ping() {
return 'ping';
}
}
class Computer extends Human {
constructor() {}
static pingpong() {
return super.ping() + ' pong';
}
}
Computer.pingpong(); // 'ping pong'
Cancellare una proprietà del super causa eccezione
Non puoi cancellare una proprietà della classe padre usando l' operatore delete e uper.prop o super[esperssione], questo causerà un ReferenceError.
class Base {
constructor() {}
foo() {}
}
class Derived extends Base {
constructor() {}
delete() {
delete super.foo;
}
}
new Derived().delete(); // ReferenceError: uso della delete con 'super'.
Super.prop non può sovrascrivere proprietà non scrivibili
Quando si definisce una proprietà non riscrivibile con ad esempio Object.defineProperty, super non può modificarne il valore.
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
Uso di super.prop in oggetti literals
Super can also be used in the object initializer / literal notation. In this example, two objects define a method. In the second object, super calls the first object's method. This works with the help of Object.setPrototypeOf() with which we are able to set the prototype of obj2 to obj1, so that super is able to find method1 on obj1.
var obj1 = {
method1() {
console.log("method 1");
}
}
var obj2 = {
method2() {
super.method1();
}
}
Object.setPrototypeOf(obj2, obj1);
obj2.method2(); // logs "method 1"
Specifications
| Specification | Status | Comment |
|---|---|---|
| ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'super' in that specification. |
Standard | Initial definition. |
| ECMAScript Latest Draft (ECMA-262) The definition of 'super' in that specification. |
Draft |
Browser compatibili
| Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
|---|---|---|---|---|---|
| Basic support | 42.0 | 45 (45) | ? | ? | ? |
| Feature | Android | Android Webview | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile | Chrome for Android |
|---|---|---|---|---|---|---|---|
| Basic support | ? | 42.0 | 45.0 (45) | ? | ? | ? | 42.0 |
Gecko specific notes
super()non funziona come previsto in prototipi built-in.