About Lesson
In this JavaScript lesson we are going to talk about JavaScript Static Functions, so static functions are defined in classes itself not on the prototype, it means that we can not call static functions on the object, but we can call it on the class name.
Now let’s create an example, you can see that we have static function in our class, you can not access to this static function using the class object.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
class Display { constructor(name) { this.name = name; } static greeting() { console.log("Hello From Static") } } let dispay = new Display("Parwiz"); console.log(dispay); //we can not call like this dispay.greeting(); |
You can access to that static function using the class name it self not the object.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
class Display { constructor(name) { this.name = name; } static greeting() { console.log("Hello From Static") } } let dispay = new Display("Parwiz"); console.log(dispay); //we can access like this Display.greeting(); |
Exercise Files
No Attachment Found