Introduction
In Object-Oriented Programming (OOP), an instance is an individual object created from a class. A class acts as a blueprint, while an instance is the actual object that contains its own data and can use the methods defined in the class.
For example, if Employee is a class, you can create multiple employee objects such as John, Alice, and David. Each of these objects is called an instance of the Employee class.
In Node.js, instances are created using the new keyword. Every instance has its own copy of the object’s properties while sharing the methods defined in the class.
For automation engineers, creating instances is essential when working with the Page Object Model (POM). Each page class is instantiated so that its methods can be used to automate browser interactions.
In this tutorial, you’ll learn how to create and use instances in Node.js.
What is an Instance?
An instance is an object created from a class.
A class is a blueprint.
An instance is the actual object created from that blueprint.
Example:
class Employee {
}
const employee =
new Employee();
Here:
Employeeis the class.employeeis an instance of the class.
Why Create Instances?
Instances allow developers to:
Create multiple objects from one class.
Store different data in each object.
Reuse the same methods.
Organize application data.
Build scalable applications.
Follow object-oriented programming principles.
Example 1: Create an Instance
class Student {
}
const student =
new Student();
console.log(student);
Sample Output
Student {}
Example 2: Create Multiple Instances
class Employee {
}
const employee1 =
new Employee();
const employee2 =
new Employee();
console.log(employee1);
console.log(employee2);
Sample Output
Employee {}
Employee {}
Although both objects are created from the same class, they are different instances.
Example 3: Instance with Properties
class Employee {
name = "John";
department = "IT";
}
const employee =
new Employee();
console.log(employee.name);
console.log(employee.department);
Sample Output
John
IT
Example 4: Instance with Methods
class Employee {
greet() {
console.log(
"Welcome Employee"
);
}
}
const employee =
new Employee();
employee.greet();
Sample Output
Welcome Employee
Example 5: Multiple Independent Instances
Each instance maintains its own state.
class Counter {
count = 0;
}
const counter1 =
new Counter();
const counter2 =
new Counter();
counter1.count = 5;
counter2.count = 10;
console.log(counter1.count);
console.log(counter2.count);
Sample Output
5
10
Example 6: Check an Instance
Use the instanceof operator to determine whether an object is an instance of a class.
class Employee {
}
const employee =
new Employee();
console.log(
employee instanceof Employee
);
Sample Output
true
Real-World Example
Create multiple product objects.
class Product {
name = "Laptop";
price = 75000;
}
const product1 =
new Product();
const product2 =
new Product();
console.log(product1.name);
console.log(product2.price);
Sample Output
Laptop
75000
Automation Testing Example
Instances are widely used in automation frameworks.
Playwright Example
Create an instance of a page object.
class LoginPage {
login() {
console.log(
"Login Successful"
);
}
}
const loginPage =
new LoginPage();
loginPage.login();
Selenium Example
Create an instance of a search page.
class SearchPage {
search() {
console.log(
"Searching Product"
);
}
}
const searchPage =
new SearchPage();
searchPage.search();
Cypress Example
Create an instance of a dashboard page.
class DashboardPage {
open() {
console.log(
"Dashboard Opened"
);
}
}
const dashboard =
new DashboardPage();
dashboard.open();
API Testing Example
Create an instance of an API client.
class ApiClient {
sendRequest() {
console.log(
"API Request Sent"
);
}
}
const api =
new ApiClient();
api.sendRequest();
Data-Driven Testing Example
Create multiple employee instances.
class Employee {
name = "Employee";
}
const emp1 =
new Employee();
const emp2 =
new Employee();
console.log(emp1.name);
console.log(emp2.name);
Common Mistakes
Forgetting the new Keyword
Incorrect:
const employee =
Employee();
Correct:
const employee =
new Employee();
Without new, JavaScript throws an error for class constructors.
Assuming Two Instances Are the Same Object
class Employee {
}
const emp1 =
new Employee();
const emp2 =
new Employee();
console.log(emp1 === emp2);
Sample Output
false
Each call to new creates a separate object.
Calling Methods Without Creating an Instance
Incorrect:
Employee.greet();
Correct:
const employee =
new Employee();
employee.greet();
Best Practices
Always use the
newkeyword when creating class instances.Use meaningful variable names for instances.
Create multiple instances when representing different objects.
Keep class definitions reusable.
Avoid creating unnecessary instances.
Use instances to access class properties and methods.
Organize instances logically within your application.
Conclusion
Creating instances is one of the most important concepts in Object-Oriented Programming. A class defines the blueprint, while an instance is the actual object created from that blueprint.
For automation engineers, creating instances is a daily practice when working with the Page Object Model (POM), API clients, utility classes, and reusable components.
Understanding how to create and use instances is essential before learning constructors, inheritance, encapsulation, polymorphism, and other advanced OOP concepts.
Frequently Asked Questions (FAQs)
What is an instance?
An instance is an object created from a class.
Which keyword is used to create an instance?
The new keyword.
Can a class have multiple instances?
Yes. A single class can create any number of independent instances.
Are two instances of the same class equal?
No. Each instance is a separate object with its own identity.
Why are instances important in automation testing?
Instances allow automation engineers to create reusable page objects, API clients, and utility classes in frameworks such as Playwright, Selenium, and Cypress.
Key Takeaways
An instance is an object created from a class.
Use the
newkeyword to create instances.A single class can create multiple independent instances.
Each instance has its own properties and can access class methods.
Use the
instanceofoperator to verify an object’s type.Each instance is a separate object in memory.
Instances are widely used in Playwright, Selenium, Cypress, API testing, and Node.js applications.
Use meaningful names for instance variables.
Reuse class definitions by creating multiple instances.
Mastering instances is essential before learning constructors and advanced OOP concepts.
