“Статическая переменная JavaScript в классе” Ответ

Статическая переменная JavaScript в классе

class Thing {
  static type = 'thing';
  static myType() {
    return `This class has a type of ${this.type}`;
  }
}
console.log(Thing.type);
//=> 'thing'
console.log(Thing.myType());
//=> 'This class has a type of thing'

// Instances do not inherit static fields and methods
const t = new Thing();
console.log(t.type);
//=> undefined
console.log(t.myType())
//=> Uncaught TypeError: t.myType is not a function 
Famous Flatworm

javascript static class переменная

class ClassWithStaticMethod {
  static staticProperty = 'someValue';
  static staticMethod() {
    return 'static method has been called.';
  }
  static {
    console.log('Class static initialization block called');
  }
}

console.log(ClassWithStaticMethod.staticProperty);
// output: "someValue"
console.log(ClassWithStaticMethod.staticMethod());
// output: "static method has been called."

//------------------syntex-------------------------

static methodName() { /* ... */ }
static propertyName [= value];

// Class static initialization block
static {

}
Easy Earthworm

Ответы похожие на “Статическая переменная JavaScript в классе”

Вопросы похожие на “Статическая переменная JavaScript в классе”

Больше похожих ответов на “Статическая переменная JavaScript в классе” по JavaScript

Смотреть популярные ответы по языку

Смотреть другие языки программирования