Czytasz angielską wersję tego artykułu, ponieważ nie ma jeszcze tłumaczenia dla tego języka. Pomóż nam przetłumaczyć ten artykuł!
Słowo kluczowe static definiuje statyczną metodę lub klasę. Metody statyczne nie są wywoływane na instancjach klasy, a bezpośrednio na samej klasie. Są to często funkcje służące na przykład do tworzenia czy klonowania obiektów.
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
Składnia
static nazwaMetody() { ... }
Opis
Wywołania metod statycznych są wykonywane bezpośrednio na klasie, a nie jej instancji.
Wywoływanie metod statycznych
Z innej metody statycznej
W przypadku wywołania metody statycznej w innej metodzie statycznej tej samej klasy, możesz użyć słowa kluczowego this.
class StaticMethodCall {
static staticMethod() {
return 'Static method has been called';
}
static anotherStaticMethod() {
return this.staticMethod() + ' from another static method';
}
}
StaticMethodCall.staticMethod();
// 'Static method has been called'
StaticMethodCall.anotherStaticMethod();
// 'Static method has been called from another static method'
Z konstruktora klasy i innych metod
Metody statyczne nie są bezpośrednio dostępne przy użyciu słowa kluczowego this w metodach niestatycznych.
W takim przypadku należy użyć nazwy klasy: NAZWA_KLASY.NAZWA_METODY_STATYCZNEJ() lub przez wywołanie metody jako właściwości konstruktora: this.constructor.NAZWA_METODY_STATYCZNEJ().
class StaticMethodCall {
constructor() {
console.log(StaticMethodCall.staticMethod());
// 'static method has been called.'
console.log(this.constructor.staticMethod());
// 'static method has been called.'
}
static staticMethod() {
return 'static method has been called.';
}
}
Przykłady
Poniższy przykład pokazuje kilka rzeczy:
- Implementacje metody statycznej w klasie.
- That a class with a static member can be sub-classed.
- Jak metoda statyczna może być — i jak nie może być — wywoływana.
class Triple {
static triple(n) {
if (n === undefined) {
n = 1;
}
return n * 3;
}
}
class BiggerTriple extends Triple {
static triple(n) {
return super.triple(n) * super.triple(n);
}
}
console.log(Triple.triple()); // 3
console.log(Triple.triple(6)); // 18
var tp = new Triple();
console.log(BiggerTriple.triple(3));
// 81 (not affected by parent's instantiation)
console.log(tp.triple());
// 'tp.triple is not a function'.
Specyfikacje
| Specyfikacja | Status | Komentarz |
|---|---|---|
| ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'Class definitions' in that specification. |
Standard | Definicja początkowa. |
| ECMAScript Latest Draft (ECMA-262) The definition of 'Class definitions' in that specification. |
Draft |
Wsparcie przeglądarek
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
static | Chrome
Full support
49
| Edge Full support 13 | Firefox Full support 45 | IE No support No | Opera Full support 36 | Safari Full support 9 | WebView Android
Full support
49
| Chrome Android
Full support
49
| Firefox Android Full support 45 | Opera Android ? | Safari iOS Full support 9 | Samsung Internet Android Full support Yes | nodejs
Full support
6.0.0
|
Legend
- Full support
- Full support
- No support
- No support
- Compatibility unknown
- Compatibility unknown
- See implementation notes.
- See implementation notes.
- User must explicitly enable this feature.
- User must explicitly enable this feature.