Monday, 26 December 2016

How Static Method is Implemented in JavaScript

Folling are the code sample for the static menthod on class.

1:- A static method is implemented on a class
class Multiplication{
  static multiply(number) {
    if (number === undefined) {
      number = 1;
    return number * number;
2:- A class with a static member can be sub-classed.
class HugeMultiplication extends Multiplication {
  static multiply(number) {
    return super.multiply(number) * super.multiply(number);
3:- A Static method can be called like below
console.log(Multiplication.multiply()); ///1
console.log(Multiplication.multiply(6)); // 36

Remark Point:-
A static method cannot be called like below
var multi = new Multiplication();
// Uncaught TypeError: multi.multiply is not a function

No comments:

Post a Comment

Note: only a member of this blog may post a comment.