“Variabel statis JavaScript di kelas” Kode Jawaban

Variabel statis JavaScript di kelas

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

variabel kelas statis javascript

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

Jawaban yang mirip dengan “Variabel statis JavaScript di kelas”

Pertanyaan yang mirip dengan “Variabel statis JavaScript di kelas”

Lebih banyak jawaban terkait untuk “Variabel statis JavaScript di kelas” di JavaScript

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya