Static Methods
This lesson explains what static methods are and how they are implemented using an example.
We'll cover the following...
What are Static Methods?
Methods that we define inside the class get assigned to the prototype object of the class and belong to all the objects instances that get created from that class.
Let’s consider the example of the class Student
:
Press + to interact
class Student {constructor(name,age,sex,marks) {this.name = namethis.age = agethis.sex = sexthis.marks = marks}//method defined in class gets assigned to the prototype of the class StudentsdisplayName(){console.log("Name is:", this.name)}}var student1 = new Student('Kate',15,'F',20)student1.displayName()console.log("Age:",student1.age)console.log("Sex:",student1.sex)console.log("Marks:",student1.marks)
The class Student
has the method displayName
defined. Any object instance created will inherit this method from Student.prototype
just as student1
inherits it.
Now, let’s suppose we have multiple ...