Static Methods
This lesson explains what static methods are and how they are defined using an example.
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
Javascript (babel-node)
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 Student
objects, and ...
Create a free account to access the full course.
By signing up, you agree to Educative's Terms of Service and Privacy Policy