Introduction
Constructors are one of the most commonly used features in Object-Oriented Programming (OOP). They automatically initialize objects when they are created, making code cleaner, more organized, and easier to maintain.
In real-world Node.js applications, constructors are used to initialize objects such as users, employees, products, bank accounts, API clients, database connections, and configuration settings.
For automation engineers, constructors play an important role in frameworks like Playwright, Selenium, and Cypress. They are used to initialize browser pages, WebDriver instances, page objects, API clients, reusable utilities, and configuration values.
In this tutorial, you’ll explore practical examples of constructors that demonstrate how they are used in real-world development and automation testing.
Example 1: Employee Management System
Create employee objects with different information.
class Employee {
constructor(id, name, department) {
this.id = id;
this.name = name;
this.department = department;
}
displayDetails() {
console.log(this.id);
console.log(this.name);
console.log(this.department);
}
}
const employee1 =
new Employee(
101,
"John",
"IT"
);
const employee2 =
new Employee(
102,
"Alice",
"HR"
);
employee1.displayDetails();
employee2.displayDetails();
Sample Output
101
John
IT
102
Alice
HR
Example 2: Student Management System
Initialize student information.
class Student {
constructor(name, course, marks) {
this.name = name;
this.course = course;
this.marks = marks;
}
showResult() {
console.log(this.name);
console.log(this.course);
console.log(this.marks);
}
}
const student =
new Student(
"David",
"Node.js",
95
);
student.showResult();
Sample Output
David
Node.js
95
Example 3: Product Inventory System
Store product details.
class Product {
constructor(name, price, stock) {
this.name = name;
this.price = price;
this.stock = stock;
}
displayProduct() {
console.log(this.name);
console.log(this.price);
console.log(this.stock);
}
}
const product =
new Product(
"Laptop",
75000,
20
);
product.displayProduct();
Sample Output
Laptop
75000
20
Example 4: Bank Account System
Initialize bank account details.
class BankAccount {
constructor(accountHolder, balance) {
this.accountHolder = accountHolder;
this.balance = balance;
}
showBalance() {
console.log(
"Balance: " + this.balance
);
}
}
const account =
new BankAccount(
"Rahul",
10000
);
account.showBalance();
Sample Output
Balance: 10000
Example 5: Library Management System
Create book objects.
class Book {
constructor(title, author) {
this.title = title;
this.author = author;
}
displayBook() {
console.log(this.title);
console.log(this.author);
}
}
const book =
new Book(
"JavaScript Guide",
"John Doe"
);
book.displayBook();
Sample Output
JavaScript Guide
John Doe
Example 6: Playwright Automation Example
Initialize a Playwright page object.
class LoginPage {
constructor(page) {
this.page = page;
}
login() {
console.log(
"Logging into application"
);
}
}
const loginPage =
new LoginPage("Browser Page");
loginPage.login();
Sample Output
Logging into application
Example 7: Selenium Automation Example
Initialize a WebDriver instance.
class HomePage {
constructor(driver) {
this.driver = driver;
}
openHomePage() {
console.log(
"Home Page Opened"
);
}
}
const homePage =
new HomePage("ChromeDriver");
homePage.openHomePage();
Sample Output
Home Page Opened
Example 8: Cypress Automation Example
Initialize test configuration.
class TestConfig {
constructor(baseUrl) {
this.baseUrl = baseUrl;
}
showUrl() {
console.log(this.baseUrl);
}
}
const config =
new TestConfig(
"https://example.com"
);
config.showUrl();
Sample Output
https://example.com
Example 9: API Testing Example
Initialize an API client.
class ApiClient {
constructor(baseUrl) {
this.baseUrl = baseUrl;
}
sendRequest() {
console.log(
"Sending request to " + this.baseUrl
);
}
}
const api =
new ApiClient(
"https://api.example.com"
);
api.sendRequest();
Sample Output
Sending request to https://api.example.com
Example 10: Data-Driven Testing Example
Initialize employee test data.
class Employee {
constructor(id, name, role) {
this.id = id;
this.name = name;
this.role = role;
}
display() {
console.log(this.id);
console.log(this.name);
console.log(this.role);
}
}
const employee =
new Employee(
201,
"Sophia",
"Tester"
);
employee.display();
Sample Output
201
Sophia
Tester
Common Mistakes
Forgetting the new Keyword
Incorrect:
const employee =
Employee(
101,
"John"
);
Correct:
const employee =
new Employee(
101,
"John"
);
Forgetting to Use this
Incorrect:
constructor(name) {
name = name;
}
Correct:
constructor(name) {
this.name = name;
}
Passing Parameters in the Wrong Order
Incorrect:
new Employee(
"IT",
101,
"John"
);
Always pass constructor parameters in the correct order.
Best Practices
-
Use constructors to initialize object properties.
-
Keep constructors simple and focused on initialization.
-
Use meaningful parameter names.
-
Use
thisto assign property values. -
Avoid placing business logic inside constructors.
-
Create reusable classes with flexible constructors.
-
Initialize all required properties during object creation.
Conclusion
Constructors are essential for creating well-structured and reusable objects in Node.js. They simplify object creation by automatically initializing properties with the required values.
In real-world applications, constructors are used in employee management systems, banking applications, inventory systems, library management systems, API clients, and many other applications.
For automation engineers, constructors are fundamental to implementing the Page Object Model (POM), initializing browser sessions, WebDriver objects, API clients, and reusable test utilities.
Mastering constructors will help you write cleaner, more maintainable, and scalable object-oriented code in Node.js.
Frequently Asked Questions (FAQs)
What is the purpose of a constructor?
A constructor initializes object properties when a new object is created.
Can constructors accept parameters?
Yes. Constructors can accept one or more parameters to initialize objects with different values.
How many constructors can a JavaScript class have?
A JavaScript class can have only one constructor.
Why are constructors useful in automation testing?
Constructors initialize page objects, browser instances, WebDriver objects, API clients, configuration values, and reusable components.
When is a constructor executed?
A constructor is automatically executed whenever an object is created using the new keyword.
Key Takeaways
-
Constructors automatically initialize objects.
-
Use constructors to reduce repetitive code.
-
Constructor parameters make classes flexible and reusable.
-
Always create objects using the
newkeyword. -
Use
thisto assign values to object properties. -
Constructors improve code organization and readability.
-
Constructors are widely used in Playwright, Selenium, Cypress, and API testing.
-
Keep constructors simple and focused on initialization.
-
Initialize all required properties during object creation.
-
Constructors are one of the core concepts of Object-Oriented Programming in Node.js.
