JavaScript static methods

Static methods in JavaScript are methods that are attached to the class itself rather than an instance of the class. They can be called directly by class name without creating an instance of the class. Static methods are typically used to perform utility functions or operations associated with a class.

In JavaScript, static methods can be defined using the `static` keyword. Here is an example:

```javascript
class MyClass {
  static myStaticMethod() {
    console.log('This is a static method.');
  }

  static anotherStaticMethod() {
    console.log('This is another static method.');
  }
}

// Call the static method
MyClass.myStaticMethod(); // Output: This is a static method.
MyClass.anotherStaticMethod(); // Output: This is another static method.
```

In the above example, `myStaticMethod()` and `anotherStaticMethod()` are both static methods of class `MyClass`. By using the class name, we can call these methods directly.

It is important to note that static methods cannot access instance properties or methods of a class because they have nothing to do with an instance of the class. They can only access other static members (such as static properties or other static methods).

Also, static methods cannot be inherited. If you derive a subclass from a class that contains static methods, the subclass will inherit the instance methods and properties of the parent class, but not the static methods of the parent class.

```javascript
class MySubClass extends MyClass {   // Here is the instance method of the subclass   myMethod() {     console.log('This is an instance method.');   } }




const instance = new MySubClass();
instance.myMethod(); // Output: This is an instance method.
MySubClass.myStaticMethod(); // Error! Static methods are not inherited by subclasses.
```

In the above example, `MySubClass` inherits the instance method `myMethod()` of `MyClass`, but cannot inherit `myStaticMethod()`.

Static methods are useful in many situations, such as creating utility functions or utility class methods that do not require access to the instance state of the class. They can provide a concise way to organize and invoke functionality associated with a class.

Here are some examples using static methods:

```javascript
class MathUtils {
  static square(x) {
    return x * x;
  }

  static getRandomNumber(min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
  }

  static isEven(number) {
    return number % 2 === 0;
  }
}

console.log(MathUtils.square(5)); // output: 25

const randomNumber = MathUtils.getRandomNumber(1, 10);
console.log(randomNumber); // output: generated random number

console.log(MathUtils.isEven(4)); // Output: true
console.log(MathUtils.isEven(7)); // Output: false
```

In the example above, the `MathUtils` class defines three static methods: `square()`, `getRandomNumber()` and `isEven()`.

- The `square()` method takes an argument `x` and returns its square.
- The `getRandomNumber()` method accepts two parameters `min` and `max` and returns a random integer between `min` and `max`.
- The `isEven()` method accepts a parameter `number` to determine whether the number is even.

These static methods can be called directly by the class name without creating an instance of the `MathUtils` class. By calling `MathUtils.square(5)`, we can calculate the square of 5 and print the result.

Using static methods can conveniently organize and call some utility functions or operations related to the class, without having to create an instance of the class every time it is used.

Guess you like

Origin blog.csdn.net/smarten57/article/details/131146266