Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Class Methods | Classes
Advanced JavaScript Mastery
course content

Course Content

Advanced JavaScript Mastery

Advanced JavaScript Mastery

1. Classes
2. DOM Manipulation
3. Events and Event Handling
4. Asynchronous JavaScript and APIs

book Class Methods

Now that we know how to add properties using the constructor, let's explore how to add methods to a class.

Defining and Using Class Methods

These methods typically operate on the object's properties and provide a way to interact with its data.

To define a class method, simply declare a function inside the class without using the function keyword.

In this example, getInfo is a method of the Animal class. Any instance of Animal can call this method to retrieve information about that particular instance.

How Methods Are Associated with Instances of a Class

When you create an instance of a class, the methods defined inside the class automatically become available to that instance. These methods can access and manipulate the instance's properties using the this keyword, which refers to the current object.

Think of methods as tools that each object (instance) carries with it. Each object has its own data (properties), but they share the same tools (methods) to interact with that data.

Let's consider the example:

1234567891011121314151617
class Animal { constructor(name, type) { this.name = name; this.type = type; } // Class method getInfo() { return `${this.name} is a ${this.type} animal.`; } } const lion = new Animal('Lion', 'Wild'); console.log(lion.getInfo()); // Output: Lion is a Wild animal. const pig = new Animal('Pig', 'Domestic'); console.log(pig.getInfo()); // Output: Pig is a Domestic animal.
copy

In this example, both lion and pig are instances of the Animal class. The getInfo method is available to both, and it returns different results based on the properties of the specific instance it's called on. This demonstrates how methods are shared across instances but operate on data specific to each one.

Why Use Class Methods?

Class methods define behaviors that are specific to the object. They make code more modular, maintainable, and create a clear separation of responsibilities. Instead of directly manipulating object properties, class methods offer a controlled way to interact with and modify the object's data.

Example with a method that changes object state:

1234567891011121314151617181920
class Animal { constructor(name, type) { this.name = name; this.type = type; } getInfo() { return `${this.name} is a ${this.type} animal.`; } changeType(newType) { this.type = newType; } } const lion = new Animal('Lion', 'Wild'); console.log(lion.getInfo()); // Output: Lion is a Wild animal. lion.changeType('Domestic'); console.log(lion.getInfo()); // Output: Lion is a Domestic animal.
copy

In this example, the changeType method allows you to update the type property of the object. This shows how methods can encapsulate logic that directly affects the object's state.

1. What is a class method?
2. In a class, how do you define a method called `speak` that an instance can use?
3. What does the following code output?
What is a class method?

What is a class method?

Select the correct answer

In a class, how do you define a method called `speak` that an instance can use?

In a class, how do you define a method called speak that an instance can use?

Select the correct answer

What does the following code output?

What does the following code output?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 4
We're sorry to hear that something went wrong. What happened?
some-alt