JavaScript Class static keyword

Definition and Usage

static The keyword is used to define static methods for class.

Static methods are called directly on the class (in the example above, Car) call, without creating an instance/object of the class (mycar)

Instance

Example 1

Create a static method and call it on the class:

class Car {
  constructor(brand) {
    this.carname = brand;
  }
  static hello() {  // static method
    return "Hello!!";
  }
}
mycar = new Car("Ford");
//Call 'hello()' on the class Car:
document.getElementById("demo").innerHTML = Car.hello();
//Do not call on the 'mycar' object:
//document.getElementById("demo").innerHTML = mycar.hello();
//Will cause an error

ကျွန်ုပ်ကိုယ်ဖြင့် စစ်ဆေးကြည့်

Example 2

If you want to use the mycar object inside a static method, you can send it as a parameter:

Send "mycar" as a parameter:
class Car {
  constructor(brand) {
    this.carname = brand;
  }
  static hello(x) {
    return "Hello " + x.carname;
  }
}
mycar = new Car("Ford");
document.getElementById("demo").innerHTML = Car.hello(mycar);

ကျွန်ုပ်ကိုယ်ဖြင့် စစ်ဆေးကြည့်

အက္ခရာ

static methodName()

နည်းပါးတည်း

ဂျိုးတိုး ပုံစံပေး အီကာစတာ 2015 (ES6)

ဘာသာစကား ကူးကြောင်း

အကြိုးအပျက် ခလုတ အိပ် ဖက်ဖရက် ဆာဖီ အိုပရက်
static 49.0 13.0 45.0 9.0 36.0

လုပ်ကြည့်ရန် စာရင်း

ဂျိုးတိုး တိုက်ရိုက်JavaScript အကျယ်အဝန်

ဂျိုးတိုး တိုက်ရိုက်ဂျိုးတိုးပေါ် အီကာစတာ 2015

JavaScript အချက်အလက်အုပ်constructor() ဓာတ်ပြက္ခဒ်