Introduction
In JavaScript, an object can store not only data but also functions. When a function is stored as a property of an object, it is called an object method.
Object methods allow objects to perform actions. They help combine both data (properties) and behavior (methods) into a single object, making code more organized and reusable.
Object methods are widely used in web development, Node.js applications, API testing, and automation frameworks such as Selenium, Playwright, and Cypress.
For automation engineers, object methods are useful for creating reusable actions such as logging into an application, launching a browser, sending API requests, validating responses, and performing common test operations.
What is an Object Method?
An object method is a function that belongs to an object.
Example:
let person = {
name: "John",
greet: function() {
console.log("Hello!");
}
};
person.greet();
Output
Hello!
In this example:
nameis a property.greet()is a method.
Why Do We Use Object Methods?
Object methods help developers:
Group related functionality.
Improve code organization.
Reuse common logic.
Represent real-world objects.
Reduce code duplication.
Build maintainable applications.
Creating an Object Method
Syntax
let objectName = {
methodName: function() {
// Code
}
};
Example
let calculator = {
add: function() {
console.log(10 + 20);
}
};
calculator.add();
Output
30
Using the this Keyword
Inside an object method, the this keyword refers to the current object.
let employee = {
name: "Alice",
greet: function() {
console.log("Welcome " + this.name);
}
};
employee.greet();
Output
Welcome Alice
Object Method Shorthand (ES6)
From ES6 onwards, JavaScript allows a shorter syntax for defining methods.
Instead of:
let person = {
greet: function() {
console.log("Hello");
}
};
You can write:
let person = {
greet() {
console.log("Hello");
}
};
person.greet();
Output
Hello
Methods with Parameters
Object methods can accept parameters.
let calculator = {
multiply(a, b) {
return a * b;
}
};
console.log(calculator.multiply(5, 4));
Output
20
Methods Returning Values
Methods can return values using the return statement.
let rectangle = {
area(length, width) {
return length * width;
}
};
console.log(rectangle.area(10, 5));
Output
50
Real-World Example
Suppose an employee object contains a method to display employee details.
let employee = {
name: "John",
department: "QA",
displayInfo() {
console.log(this.name + " works in " + this.department);
}
};
employee.displayInfo();
Output
John works in QA
Another example:
Store product details.
let product = {
name: "Laptop",
price: 65000,
showPrice() {
console.log(this.price);
}
};
product.showPrice();
Output
65000
Automation Testing Example
Automation engineers frequently use object methods to create reusable automation actions.
Playwright Example
Launch a browser.
const browser = {
launch() {
console.log("Launching Chromium");
}
};
browser.launch();
Output
Launching Chromium
Selenium Example
Open a browser.
const selenium = {
openBrowser() {
console.log("Opening Chrome Browser");
}
};
selenium.openBrowser();
Output
Opening Chrome Browser
Cypress Example
Log in to an application.
const user = {
login() {
console.log("User Logged In");
}
};
user.login();
Output
User Logged In
API Testing Example
Send an API request.
const api = {
sendRequest() {
console.log("Sending GET Request");
}
};
api.sendRequest();
Output
Sending GET Request
Data-Driven Testing Example
Validate test data.
const validator = {
validate() {
console.log("Validation Successful");
}
};
validator.validate();
Output
Validation Successful
Common Mistakes
Forgetting Parentheses While Calling a Method
Incorrect:
person.greet;
This returns the function itself instead of executing it.
Correct:
person.greet();
Using an Arrow Function with this
Incorrect:
let person = {
name: "John",
greet: () => {
console.log(this.name);
}
};
Arrow functions do not have their own this binding, which can lead to unexpected results.
Use a regular function or method shorthand when you need to access the object’s properties using this.
Forgetting the return Statement
Incorrect:
let calculator = {
add(a, b) {
a + b;
}
};
Correct:
let calculator = {
add(a, b) {
return a + b;
}
};
Best Practices
Use Method Shorthand
The ES6 method shorthand is shorter and easier to read.
Use this for Object Properties
Access object properties using this inside methods.
Keep Methods Focused
Each method should perform one specific task.
Use Meaningful Method Names
Instead of:
obj.a();
Use:
employee.displayInfo();
Meaningful names make the code easier to understand.
Conclusion
Object methods allow JavaScript objects to perform actions by storing functions as properties. They help combine data and behavior into a single structure, making applications more organized, reusable, and easier to maintain.
For automation engineers, object methods are essential for building reusable automation utilities such as browser operations, login actions, API requests, validation functions, and test workflows. Mastering object methods is an important step toward writing clean and scalable JavaScript automation code.
Frequently Asked Questions (FAQs)
What is an object method?
An object method is a function stored as a property of an object.
How do you call an object method?
objectName.methodName();
What does the this keyword refer to?
Inside an object method, this refers to the current object.
Can object methods accept parameters?
Yes.
calculator.add(10, 20);
Can object methods return values?
Yes. They can use the return statement to return a value.
Why are object methods important in automation testing?
Automation engineers use object methods to create reusable actions such as browser launches, login operations, API requests, response validation, and other common automation tasks.
Key Takeaways
Object methods are functions stored inside objects.
Methods allow objects to perform actions.
The
thiskeyword refers to the current object.ES6 provides a shorter method declaration syntax.
Methods can accept parameters and return values.
Call methods using parentheses
().Use meaningful method names for readability.
Object methods improve code organization and reusability.
They are widely used in JavaScript development and automation testing.
Mastering object methods is essential for building scalable and maintainable applications.
