Home >>Javascript Tutorial >JavaScript static Method
The JavaScript delivers static methods that belongs to a class instead of an instance of that same class. Hence, an instance is not necessary to call the static method. Point to be noted is that these methods are called directly on the class.
Please note that these following points should be kept in mind:
1. Here is an example of simple static method:
<script> class Test { static display() { return "static method is great" } } document.writeln(Test.display()); </script>
2. Here is example depicting the invoking of more than one static method:
<script> class Test { static display1() { return "static method is used" } static display2() { return "static method is used again" } } document.writeln(Test.display1()+"<br>"); document.writeln(Test.display2()); </script>
3. Here is an example to invoke more than one static method with similar names.
<script> class Test { static display() { return "static method is used" } static display() { return "static method is used again" } } document.writeln(Test.display()); </script>
4. Here is an example to invoke a static method within the constructor.
<script> class Test { constructor() { document.writeln(Test.display()+"<br>"); document.writeln(this.constructor.display()); } static display() { return "static method is used" } } var t=new Test(); </script>
5. Here is an example to invoke a static method within the non-static method.
<script> class Test { static display() { return "static method is used" } show() { document.writeln(Test.display()+"<br>"); } } var t=new Test(); t.show(); </script>