Class Methods

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

Defining Methods in a Class

A class constructor encapsulates all 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 a method, 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:

Get hands-on with 1200+ tech skills courses.