Introduction
Constructor parameters are values passed to a class constructor when creating a new object. These parameters allow each object (instance) to be initialized with different data, making classes flexible and reusable.
Instead of assigning the same values to every object, constructor parameters let you provide custom values during object creation.
For example, when creating an Employee object, you can pass different values for name, id, and department so that each employee object contains unique information.
In Node.js, constructor parameters are widely used to initialize objects with user input, configuration settings, database records, API data, and application-specific values.
For automation engineers, constructor parameters are commonly used to pass browser instances, page objects, WebDriver objects, API clients, configuration values, test data, and reusable dependencies in frameworks like Playwright, Selenium, and Cypress.
In this tutorial, you’ll learn how to use constructor parameters effectively in Node.js.
What are Constructor Parameters?
Constructor parameters are variables declared inside the constructor() method that receive values when an object is created using the new keyword.
These values are usually assigned to object properties using the this keyword.
Syntax
class ClassName {
constructor(parameter1, parameter2) {
this.parameter1 = parameter1;
this.parameter2 = parameter2;
}
}
const object =
new ClassName(
value1,
value2
);
Why Use Constructor Parameters?
Constructor parameters help developers:
Initialize objects with custom values.
Create reusable classes.
Reduce repetitive code.
Improve code flexibility.
Represent real-world entities.
Pass data during object creation.
Build scalable applications.
Example 1: Constructor with One Parameter
class Student {
constructor(name) {
this.name = name;
}
}
const student =
new Student("David");
console.log(student.name);
Sample Output
David
Example 2: Constructor with Multiple Parameters
class Employee {
constructor(name, department) {
this.name = name;
this.department = department;
}
}
const employee =
new Employee(
"Alice",
"HR"
);
console.log(employee.name);
console.log(employee.department);
Sample Output
Alice
HR
Example 3: Different Objects with Different Values
class Employee {
constructor(name) {
this.name = name;
}
}
const employee1 =
new Employee("John");
const employee2 =
new Employee("Emma");
console.log(employee1.name);
console.log(employee2.name);
Sample Output
John
Emma
Example 4: Constructor with Default Parameters
class User {
constructor(name = "Guest") {
this.name = name;
}
}
const user1 =
new User();
const user2 =
new User("Rahul");
console.log(user1.name);
console.log(user2.name);
Sample Output
Guest
Rahul
Example 5: Constructor with Multiple Properties
class Product {
constructor(name, price, brand) {
this.name = name;
this.price = price;
this.brand = brand;
}
}
const product =
new Product(
"Laptop",
75000,
"Dell"
);
console.log(product.name);
console.log(product.price);
console.log(product.brand);
Sample Output
Laptop
75000
Dell
Example 6: Constructor Parameters with Methods
class Employee {
constructor(name) {
this.name = name;
}
greet() {
console.log(
"Welcome " + this.name
);
}
}
const employee =
new Employee("John");
employee.greet();
Sample Output
Welcome John
Real-World Example
Create a bank account.
class BankAccount {
constructor(accountHolder, balance) {
this.accountHolder = accountHolder;
this.balance = balance;
}
showDetails() {
console.log(this.accountHolder);
console.log(this.balance);
}
}
const account =
new BankAccount(
"Rahul",
10000
);
account.showDetails();
Sample Output
Rahul
10000
Automation Testing Example
Constructor parameters are widely used in automation frameworks.
Playwright Example
Pass the Playwright page object.
class LoginPage {
constructor(page) {
this.page = page;
}
}
const loginPage =
new LoginPage("Browser Page");
console.log(loginPage.page);
Selenium Example
Pass the WebDriver object.
class HomePage {
constructor(driver) {
this.driver = driver;
}
}
const homePage =
new HomePage("ChromeDriver");
console.log(homePage.driver);
Cypress Example
Pass configuration values.
class TestConfig {
constructor(baseUrl) {
this.baseUrl = baseUrl;
}
}
const config =
new TestConfig(
"https://example.com"
);
console.log(config.baseUrl);
API Testing Example
Pass the API base URL.
class ApiClient {
constructor(baseUrl) {
this.baseUrl = baseUrl;
}
}
const api =
new ApiClient(
"https://api.example.com"
);
console.log(api.baseUrl);
Data-Driven Testing Example
Pass employee data.
class Employee {
constructor(id, name, department) {
this.id = id;
this.name = name;
this.department = department;
}
}
const employee =
new Employee(
101,
"John",
"IT"
);
console.log(employee.id);
console.log(employee.name);
console.log(employee.department);
Common Mistakes
Forgetting to Pass Required Parameters
class Employee {
constructor(name) {
this.name = name;
}
}
const employee =
new Employee();
console.log(employee.name);
Sample Output
undefined
Forgetting to Use this
Incorrect:
class Employee {
constructor(name) {
name = name;
}
}
Correct:
class Employee {
constructor(name) {
this.name = name;
}
}
Passing Parameters in the Wrong Order
Incorrect:
new Employee(
"IT",
"John"
);
If the constructor expects (name, department), passing values in the wrong order produces incorrect results.
Best Practices
Use meaningful parameter names.
Pass parameters in the correct order.
Use default values when appropriate.
Initialize all required properties inside the constructor.
Keep constructors simple and focused.
Avoid passing unnecessary parameters.
Validate parameter values when required.
Conclusion
Constructor parameters make classes flexible by allowing each object to be initialized with different values. They help reduce repetitive code, improve readability, and make applications easier to maintain.
For automation engineers, constructor parameters are widely used to pass browser instances, page objects, WebDriver objects, API clients, configuration values, and test data.
Understanding constructor parameters is essential before learning advanced Object-Oriented Programming concepts such as inheritance, encapsulation, and polymorphism.
Frequently Asked Questions (FAQs)
What are constructor parameters?
Constructor parameters are values passed to the constructor() method when creating an object.
Why are constructor parameters useful?
They allow different objects of the same class to be initialized with different values.
Can constructors have multiple parameters?
Yes. A constructor can accept one or more parameters.
Can constructor parameters have default values?
Yes. JavaScript supports default parameter values in constructors.
Why are constructor parameters important in automation testing?
They are used to pass browser instances, page objects, WebDriver objects, API clients, configuration settings, and test data, making automation code more reusable and maintainable.
Key Takeaways
Constructor parameters receive values during object creation.
Use the
newkeyword to pass constructor parameters.Constructor parameters make classes reusable and flexible.
Assign parameter values to object properties using
this.Constructors can accept multiple parameters.
Constructors can use default parameter values.
Pass parameters in the correct order.
Constructor parameters are widely used in Playwright, Selenium, Cypress, API testing, and Node.js applications.
Keep constructors simple and focused on object initialization.
Mastering constructor parameters is essential for effective Object-Oriented Programming in Node.js.
