Class Methods

This lesson teaches how to define methods inside a class and also explains the use of get/set methods in classes.

Defining Methods in a Class

In the previous lesson, the employee class constructor encapsulated all the properties and methods. However, methods can also be defined outside the constructor in a class.

Whenever a method is declared inside a class, it gets defined on the prototype of that class. Hence, whenever an object instance accesses it, it gets taken from the respective class’s prototype.

Let’s take a look at how that is done.

Example

The following example demonstrates how to define methods outside of the constructor in a class:

Create a free account to access the full course.

By signing up, you agree to Educative's Terms of Service and Privacy Policy