static
Słowo kluczowe static
definiuje statyczną metodę lub właściwość klasy. Metody i właściwości statyczne nie są wywoływane na instancjach klasy, a bezpośrednio na samej klasie. Statyczne metody to często funkcje służące na przykład do tworzenia czy klonowania obiektów, a statyczne właściwości są użyteczne do cache'ów, stałej konfiguracji lub innych właściwości, które nie muszą być powielane w instancjach.
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() { ... } static nazwaWlasciwosci [=wartosc];
Przykłady
Używanie static
w klasach
Poniższy przykład demonstruje kilka rzeczy:
- Jak statyczna metoda lub właściwość jest implementowana w klasie
- Klasa z metodą lub właściwością statyczną może być dziedziczona
- Jak metoda lub właściwość statyczna może być wywoływana
class Triple {
static customName = 'Tripler';
static description = 'I triple any number you provide';
static triple(n = 1) {
return n * 3;
}
}
class BiggerTriple extends Triple {
static longDescription;
static description = 'I square the triple of any number you provide';
static triple(n) {
return super.triple(n) * super.triple(n);
}
}
console.log(Triple.description); // 'I triple any number you provide'
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(BiggerTriple.description); // 'I square the triple of any number you provide'
console.log(BiggerTriple.longDescription); // undefined
console.log(BiggerTriple.customName); // 'Tripler'
console.log(tp.triple()); // 'tp.triple is not a function'.
Wywoływanie metod statycznych z innych metod statycznych
W celu wywołania metody lub właściwości statycznej z innej metody statycznej tej samej klasy można użyć słowa kluczowego this
.
class StaticMethodCall {
static staticProperty = 'static property';
static staticMethod() {
return 'Static method and ' + this.staticProperty + ' has been called';
}
static anotherStaticMethod() {
return this.staticMethod() + ' from another static method';
}
}
StaticMethodCall.staticMethod();
// 'Static method and static property has been called'
StaticMethodCall.anotherStaticMethod();
// 'Static method and static property has been called from another static method'
Wywoływanie metod statycznych z konstruktora i innych metod
Metody statyczne nie są dostępne przez this w metodach niestatycznych. Trzeba je wywołać, używając nazwy klasy: CLASSNAME.STATIC_METHOD_NAME()
/ CLASSNAME.STATIC_PROPERTY_NAME
lub jako metody właściwości constructor
: this.constructor.STATIC_METHOD_NAME()
/ this.constructor.STATIC_PROPERTY_NAME
.
class StaticMethodCall {
constructor() {
console.log(StaticMethodCall.staticProperty); // 'static property'
console.log(this.constructor.staticProperty); // 'static property'
console.log(StaticMethodCall.staticMethod()); // 'static method has been called.'
console.log(this.constructor.staticMethod()); // 'static method has been called.'
}
static staticProperty = 'static property';
static staticMethod() {
return 'static method has been called.';
}
}
Specyfikacje
Specyfikacja | Status | Komentarz |
---|---|---|
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'Class definitions' in that specification. |
Standard | Definicja początkowa. |
ECMAScript (ECMA-262) The definition of 'Class definitions' in that specification. |
Living Standard |
Kompatybilność
BCD tables only load in the browser