Methods

Introduction

In Object-Oriented Programming (OOP), a method is a function that belongs to an object or a class. While properties store data, methods define the actions or behavior that an object can perform.

For example, an Employee object may have properties such as name and department, and methods like work(), login(), or displayDetails().

Methods help organize related functionality inside objects, making code cleaner, reusable, and easier to maintain.

In Node.js, methods are widely used in classes, objects, modules, APIs, and automation frameworks.

For automation engineers, methods are commonly used to perform actions such as logging in, clicking buttons, filling forms, sending API requests, validating responses, and handling reusable test operations in frameworks like Playwright, Selenium, and Cypress.

In this tutorial, you’ll learn how to create and use methods in Node.js.


What is a Method?

A method is a function defined inside an object or a class.

Methods perform actions using the object’s data.

Example:

const employee = {

    greet() {

        console.log(
            "Welcome!"
        );

    }

};

Here, greet() is a method.


Why Use Methods?

Methods help developers:

  • Perform actions

  • Organize related functionality

  • Improve code readability

  • Reuse logic

  • Reduce code duplication

  • Support object-oriented programming

  • Build maintainable applications


Example 1: Create a Method

const employee = {

    greet() {

        console.log(
            "Welcome Employee"
        );

    }

};

employee.greet();

Sample Output

Welcome Employee

Example 2: Method Using Object Properties

const employee = {

    name: "John",

    greet() {

        console.log(
            "Welcome " + this.name
        );

    }

};

employee.greet();

Sample Output

Welcome John

Example 3: Method with Parameters

const calculator = {

    add(a, b) {

        return a + b;

    }

};

console.log(
    calculator.add(10, 20)
);

Sample Output

30

Example 4: Method Returning a Value

const student = {

    getCourse() {

        return "Node.js";

    }

};

console.log(
    student.getCourse()
);

Sample Output

Node.js

Example 5: Multiple Methods

const calculator = {

    add(a, b) {

        return a + b;

    },

    subtract(a, b) {

        return a - b;

    }

};

console.log(
    calculator.add(20, 5)
);

console.log(
    calculator.subtract(20, 5)
);

Sample Output

25
15

Example 6: Method Inside a Class

class Employee {

    greet() {

        console.log(
            "Hello Employee"
        );

    }

}

const employee =
    new Employee();

employee.greet();

Sample Output

Hello Employee

Example 7: Using this Inside a Method

The this keyword refers to the current object.

const product = {

    name: "Laptop",

    display() {

        console.log(
            this.name
        );

    }

};

product.display();

Sample Output

Laptop

Real-World Example

Represent a bank account.

const account = {

    balance: 5000,

    showBalance() {

        console.log(
            "Balance: " + this.balance
        );

    }

};

account.showBalance();

Sample Output

Balance: 5000

Automation Testing Example

Methods are widely used in automation frameworks.

Playwright Example

Create a reusable login method.

class LoginPage {

    login() {

        console.log(
            "Login Successful"
        );

    }

}

const page =
    new LoginPage();

page.login();

Selenium Example

Create a reusable search method.

class SearchPage {

    searchProduct() {

        console.log(
            "Product Searched"
        );

    }

}

const page =
    new SearchPage();

page.searchProduct();

Cypress Example

Create a navigation method.

const dashboard = {

    open() {

        console.log(
            "Dashboard Opened"
        );

    }

};

dashboard.open();

API Testing Example

Create a request method.

const api = {

    sendRequest() {

        console.log(
            "Request Sent"
        );

    }

};

api.sendRequest();

Data-Driven Testing Example

Display employee details.

const employee = {

    name: "John",

    display() {

        console.log(
            this.name
        );

    }

};

employee.display();

Common Mistakes

Forgetting Parentheses When Calling a Method

Incorrect:

employee.greet;

Correct:

employee.greet();

Forgetting the this Keyword

Incorrect:

const employee = {

    name: "John",

    greet() {

        console.log(name);

    }

};

Correct:

const employee = {

    name: "John",

    greet() {

        console.log(
            this.name
        );

    }

};

Calling a Non-Existing Method

Incorrect:

employee.login();

If the login() method does not exist, JavaScript throws an error.

Always ensure the method is defined before calling it.


Best Practices

  • Use meaningful method names.

  • Keep methods focused on a single task.

  • Use this to access object properties.

  • Avoid duplicating code by creating reusable methods.

  • Use methods to perform object-specific actions.

  • Keep methods short and easy to understand.

  • Group related methods inside the same object or class.


Conclusion

Methods define the behavior of objects in Node.js. They allow objects and classes to perform actions, process data, and interact with other parts of an application.

For automation engineers, methods are the foundation of reusable automation code. They are used to perform actions such as logging in, clicking buttons, validating results, sending API requests, and interacting with web pages.

Mastering methods is an important step toward understanding advanced Object-Oriented Programming concepts such as constructors, inheritance, polymorphism, and encapsulation.


Frequently Asked Questions (FAQs)

What is a method?

A method is a function that belongs to an object or a class.


What is the difference between a property and a method?

A property stores data, while a method performs an action.


What does the this keyword refer to?

Inside a method, this refers to the current object.


Can methods accept parameters?

Yes. Methods can accept one or more parameters just like regular functions.


Why are methods important in automation testing?

Methods help create reusable actions such as logging in, clicking buttons, filling forms, validating responses, navigating pages, and sending API requests in frameworks like Playwright, Selenium, and Cypress.


Key Takeaways

  • Methods are functions that belong to objects or classes.

  • Methods define the behavior of an object.

  • Use this to access object properties inside methods.

  • Methods can accept parameters and return values.

  • Group related methods together for better organization.

  • Use meaningful method names that describe the action performed.

  • Methods improve code reusability and maintainability.

  • They are widely used in Playwright, Selenium, Cypress, API testing, and Node.js applications.

  • Reusable methods reduce code duplication in automation frameworks.

  • Mastering methods is essential before learning advanced Object-Oriented Programming concepts.