Introduction
Encapsulation is one of the four fundamental principles of Object-Oriented Programming (OOP). It is the practice of bundling data (properties) and methods (functions) inside a class while controlling how that data is accessed and modified.
In JavaScript, public properties are the default type of properties. They can be accessed and modified from anywhere in the program using an object of the class.
Public properties are useful when the data is intended to be freely available to other parts of the application. However, for sensitive information, private properties should be used instead.
For automation engineers, public properties are commonly used to store reusable information such as application URLs, page names, browser names, test data, API endpoints, configuration values, and other non-sensitive information.
In this tutorial, you’ll learn how to create and use public properties in Node.js.
What are Public Properties?
Public properties are properties that can be accessed and modified from outside the class.
By default, all properties created using this.propertyName are public.
Why Use Public Properties?
Public properties help developers:
Share object data easily.
Store reusable information.
Improve code readability.
Simplify object access.
Reduce unnecessary methods.
Build reusable classes.
Make objects easy to work with.
Syntax
class Employee {
constructor() {
this.name = "John";
}
}
const employee =
new Employee();
console.log(employee.name);
Example 1: Basic Public Property
class Student {
constructor() {
this.name = "David";
}
}
const student =
new Student();
console.log(student.name);
Sample Output
David
Example 2: Multiple Public Properties
class Employee {
constructor() {
this.id = 101;
this.name = "John";
this.department = "IT";
}
}
const employee =
new Employee();
console.log(employee.id);
console.log(employee.name);
console.log(employee.department);
Sample Output
101
John
IT
Example 3: Modifying Public Properties
class Product {
constructor() {
this.name = "Laptop";
}
}
const product =
new Product();
product.name = "Desktop";
console.log(product.name);
Sample Output
Desktop
Public properties can be modified directly from outside the class.
Example 4: Public Properties with Constructor Parameters
class User {
constructor(name, age) {
this.name = name;
this.age = age;
}
}
const user =
new User(
"Rahul",
25
);
console.log(user.name);
console.log(user.age);
Sample Output
Rahul
25
Example 5: Real-World Example
Store employee information.
class Employee {
constructor(id, name) {
this.id = id;
this.name = name;
}
}
const employee =
new Employee(
201,
"Sophia"
);
console.log(employee.id);
console.log(employee.name);
Sample Output
201
Sophia
Automation Testing Example
Public properties are commonly used in automation frameworks.
Playwright Example
Store the application URL.
class BasePage {
constructor() {
this.baseUrl =
"https://example.com";
}
}
const page =
new BasePage();
console.log(page.baseUrl);
Selenium Example
Store the browser name.
class BrowserConfig {
constructor() {
this.browser =
"Chrome";
}
}
const config =
new BrowserConfig();
console.log(config.browser);
Cypress Example
Store environment information.
class TestConfig {
constructor() {
this.environment =
"QA";
}
}
const config =
new TestConfig();
console.log(config.environment);
API Testing Example
Store the API endpoint.
class ApiClient {
constructor() {
this.baseUrl =
"https://api.example.com";
}
}
const api =
new ApiClient();
console.log(api.baseUrl);
Data-Driven Testing Example
Store test data.
class TestData {
constructor() {
this.username =
"admin";
this.password =
"admin123";
}
}
const data =
new TestData();
console.log(data.username);
console.log(data.password);
Common Mistakes
Forgetting this
Incorrect:
class Student {
constructor() {
name = "John";
}
}
Correct:
class Student {
constructor() {
this.name = "John";
}
}
Accessing Properties Without an Object
Incorrect:
console.log(name);
Correct:
const student =
new Student();
console.log(student.name);
Using Public Properties for Sensitive Data
Avoid storing confidential information such as passwords, API secrets, or authentication tokens as public properties.
Best Practices
Use public properties for non-sensitive data.
Initialize public properties in the constructor.
Use meaningful property names.
Keep related properties together.
Avoid exposing confidential information.
Use private properties when data should not be accessed directly.
Document important public properties.
Conclusion
Public properties are the simplest way to store and access data inside JavaScript classes. Since they are accessible from anywhere in the program, they are ideal for sharing non-sensitive information between different parts of an application.
For automation engineers, public properties are commonly used to store application URLs, browser names, page titles, test data, API endpoints, and configuration settings.
Understanding public properties is the first step toward mastering encapsulation and learning how to protect data using private properties, getters, and setters.
Frequently Asked Questions (FAQs)
What are public properties?
Public properties are class properties that can be accessed and modified from outside the class.
Are public properties the default in JavaScript?
Yes. Properties created using this.propertyName are public by default.
Can public properties be modified?
Yes. They can be read and updated from outside the class.
Should sensitive information be stored in public properties?
No. Sensitive information should be stored using private properties or other secure techniques.
Why are public properties useful in automation testing?
They provide an easy way to store reusable values such as application URLs, browser names, test data, API endpoints, and configuration settings.
Key Takeaways
Public properties are accessible from anywhere in the program.
They are the default type of properties in JavaScript classes.
Public properties are created using
this.propertyName.They can be read and modified directly.
Use public properties for non-sensitive information.
Avoid storing confidential data in public properties.
Public properties improve code readability and reusability.
They are widely used in Playwright, Selenium, Cypress, and API testing frameworks.
Initialize public properties inside constructors.
Public properties are an important part of encapsulation in Node.js.
