JavaScript: Public, Private and Protected

Bharath Kumar Murugan
3 min readApr 21, 2023

--

JavaScript is an object-oriented programming language that allows developers to create objects with properties and methods. In JavaScript, objects have properties that can be either public, private, or protected. Understanding the differences between these types of properties is essential to creating secure and efficient code.

Public Properties

Public properties are accessible to anyone who can access the object. They are defined using the this keyword and can be accessed outside of the object using the dot notation. Public properties are commonly used to store information that needs to be shared with other parts of the code. Here is an example of a public property:

function Person(name) {
this.name = name; // Public property
}

var john = new Person("John");
console.log(john.name); // Output: John

In this example, we have defined a Person object with a public name property. The name property is accessible outside of the object using the dot notation.

Private Properties

Private properties are not accessible outside of the object. They are defined using the let or const keywords and can only be accessed within the object using closures. Private properties are commonly used to store information that should not be exposed to other parts of the code. Here is an example of a private property:

function Person(name, age) {
const ageLimit = 18; // Private property
let isAdult = age >= ageLimit; // Private property

this.name = name; // Public property

this.isAdult = function() {
return isAdult;
}
}

var john = new Person("John", 25);
console.log(john.isAdult()); // Output: true
console.log(john.ageLimit); // Output: undefined

In this example, we have defined a Person object with private ageLimit and isAdult properties. The ageLimit property is a constant that cannot be changed outside of the object. The isAdult property is a boolean value that is calculated based on the age property passed into the constructor. The isAdult property is exposed outside of the object using a public method.

Protected Properties

Protected properties are accessible only to the object and its descendants. They are defined using the _ symbol at the beginning of the property name. Protected properties are commonly used to store information that needs to be accessed by child objects. Here is an example of a protected property:

function Animal() {
this._legs = 4; // Protected property
}

function Dog() {
Animal.call(this); // Inherit Animal properties

this.bark = function() {
console.log("Woof!");
}
}

var fido = new Dog();
console.log(fido._legs); // Output: 4

In this example, we have defined an Animal object with a protected _legs property. The Dog object inherits the _legs property from the Animal object using the call() method. The _legs property is accessible within the Dog object and its descendants, but not outside of the object.

In conclusion, understanding the differences between public, private, and protected properties in JavaScript is crucial to writing secure and efficient code. Public properties are accessible outside of the object, private properties are not, and protected properties are only accessible to the object and its descendants. By using these property types effectively, developers can create objects that are both flexible and secure.

--

--

Bharath Kumar Murugan
0 Followers

FrontEnd Developer | Javascript Enthusiast | Linux | Docker