This translation is incomplete. Please help translate this article from English.
Ключовата дума static дефинира статичен метод за клас. Static methods aren't called on instances of the class. Instead, they're called on the class itself. Това често са помощни функции, като например функции за създаване или клониране на обекти.
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.
Синтаксис
static methodName() { ... }
Описание
Статичните методи се правят директно в класът и не могат да се извикват като инстанции на клас.
Извикване на статичен метод
От друг статичен метод
За да извикате статичен метод в рамките на друг статичен метод от същия клас, може да използвате ключовата дума 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'
От конструктора на клас и други методи
Статичните методи не са пряко достъпни, когато се използва ключовата дума this от нестатични методи. Трябва да ги извикате, използвайки името на класа: CLASSNAME.STATIC_METHOD_NAME() или да извикате метода като собственост на constructor: this.constructor.STATIC_METHOD_NAME().
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.';
}
}
Примери
Следният пример показва няколко неща:
- Как статичните методи се изпълняват в класът.
- Това , че клас със статичен член може да бъде под-класиран.
- Как статичният метод може и не може да бъде извикан.
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'.
Спецификации
| Спецификации | Статус | Коментар |
|---|---|---|
| ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'Class definitions' in that specification. |
Standard | Initial definition. |
| ECMAScript Latest Draft (ECMA-262) The definition of 'Class definitions' in that specification. |
Draft |
Съвместимост с браузера
| 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 ? | Chrome Android Full support Yes | Edge Mobile Full support 13 | 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.

