Object-oriented programming forms the foundation of many modern software applications. A central concept in this programming paradigm are methods, which function as functions within objects. In this tutorial, you will learn how to define and apply methods to enhance your programming skills.
Main insights
- Methods are functions in an object-oriented context.
- There are various ways to define methods in JavaScript.
- The keyword this plays a crucial role in accessing object properties.
Step-by-step guide
1. Defining a method
A method is defined in JavaScript as a function placed within an object. Let's take a first example of a function called add.
Here, it simply returns the sum of A and B. You can now anchor this method within an object.

2. Creating an object with a method
Now we will create an object named Math. Within this object, we will assign the add method.
Note that we only assign the function name, not the function call. This is important for ensuring correct functionality.
3. Calling a method
This structure allows you to use the method clearly.

4. Defining a method directly in the object
This approach is also valid and gives you another way to structure your methods.
5. Introduction to the keyword this
The keyword this is a central aspect of object-oriented programming in JavaScript. It refers to the current object you are working with.
In this definition, getName() can return the full name of the person by referencing the properties firstName and lastName within the method.
6. Calling methods and accessing properties
Here, the method getName uses the keyword this to access the properties of the person object.
7. Flexibility through the keyword this
An important feature of this is that it dynamically adjusts to the respective instance. If you replace the person object with another object, this will automatically refer to the new values.

Summary – Methods in Software Programming: Beginner Tutorial
In summary, methods in object-oriented programming are indispensable. They allow for clear structuring of your code and efficient access to object properties. Once you understand the basics, you can use them for more complex applications and benefit from flexibility.
Frequently asked questions
What are methods in JavaScript?Methods are functions defined within objects that can access their properties.
How do I define a method in an object?You can define a method either as a functional reference within an object or directly as a property within the object.
What does the keyword this do?this refers to the current object being accessed and allows access to its properties.
What is the difference between a method and a function?A method is a function that is bound to an object. Functions are independent and can be written anywhere in the code.
How can I access properties within a method?You can use this to access the properties of the current object.