Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Adding Methods to Classes | ES6 Class Fundamentals
JavaScript Classes and OOP Foundations

bookAdding Methods to Classes

When you define a method inside a JavaScript class, you are creating an instance method. This means every object created from that class will have access to the method, but the method itself is shared by all instances rather than being duplicated for each one. Defining methods in the class body (outside the constructor) attaches them to the class's prototype. This makes your code more efficient, since only one copy of the method exists in memory, regardless of how many objects you create from the class.

To add a method, simply write its name followed by parentheses and a code block within the class definition—there is no need for the function keyword. These methods can access the instance's properties using the this keyword.

123456789101112
class Person { constructor(name) { this.name = name; } greet() { return "Hello, my name is " + this.name + "!"; } } const alice = new Person("Alice"); console.log(alice.greet()); // Output: Hello, my name is Alice!
copy
question mark

Which of the following statements best describes the difference between an instance method and an instance property in a JavaScript class?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 1. Kapitel 5

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

Awesome!

Completion rate improved to 6.25

bookAdding Methods to Classes

Swipe um das Menü anzuzeigen

When you define a method inside a JavaScript class, you are creating an instance method. This means every object created from that class will have access to the method, but the method itself is shared by all instances rather than being duplicated for each one. Defining methods in the class body (outside the constructor) attaches them to the class's prototype. This makes your code more efficient, since only one copy of the method exists in memory, regardless of how many objects you create from the class.

To add a method, simply write its name followed by parentheses and a code block within the class definition—there is no need for the function keyword. These methods can access the instance's properties using the this keyword.

123456789101112
class Person { constructor(name) { this.name = name; } greet() { return "Hello, my name is " + this.name + "!"; } } const alice = new Person("Alice"); console.log(alice.greet()); // Output: Hello, my name is Alice!
copy
question mark

Which of the following statements best describes the difference between an instance method and an instance property in a JavaScript class?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 1. Kapitel 5
some-alt