JavaScript Objects and Classes – Study Guide
This document covers JavaScript objects and classes, essential concepts for writing structured,
reusable code in test automation with Playwright.
1■■ What is an Object?
An object is a collection of key–value pairs that allows grouping related data and behavior together.
Example:
const user = { name: 'Alice', role: 'Tester', isActive: true };
[Link]([Link]); // Alice
Common Operations:
1 Add a property: [Link] = 28;
2 Update a property: [Link] = 'QA Engineer';
3 Delete a property: delete [Link];
4 Loop through properties: for (const key in user) { [Link](key, user[key]); }
2■■ Nested Objects
Objects can contain other objects, useful for structured data.
Example: const employee = { name: 'Bob', address: { city: 'London', zip: 'E14' } };
3■■ Object Methods
Functions defined inside objects are called methods.
Example:
const user = { name: 'Alice', greet() { [Link](`Hello, I'm ${[Link]}`); } };
[Link](); // Hello, I'm Alice
4■■ JavaScript Classes
A class is a blueprint for creating objects with shared structure and behavior.
Example:
class User { constructor(name, role) { [Link] = name; [Link] = role; } greet() {
[Link](`Hello, my name is ${[Link]}, and I am a ${[Link]}.`); } }
5■■ Inheritance (Extending a Class)
One class can inherit from another using 'extends'. The child class can reuse or extend the parent's
behavior.
Example:
class Tester extends Person { constructor(name, project) { super(name); [Link] = project; }
testApp() { [Link](`${[Link]} is testing ${[Link]}`); } }
6■■ Combining Objects and Classes
Objects often hold data, while classes define logic. Combining them leads to clean and reusable
code.
Example:
const testData = { users: [{ name: 'qa1', role: 'Tester' }, { name: 'admin1', role: 'Admin' }] };
class User { constructor(name, role) { [Link] = name; [Link] = role; } login() {
[Link](`${[Link]} logged in as ${[Link]}`); } }
■ Quick Recap
1 Object: Collection of key–value pairs
2 Method: Function inside an object
3 Class: Blueprint for creating objects
4 Constructor: Initializes new objects
5 Inheritance: One class extends another
6 this: Refers to the current object