JavaScript: Public, Private and Protected

JavaScript: Public, Private and Protected

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的更多文章

  • Functional Programming in JavaScript

    Functional Programming in JavaScript

    Functional Programming is a programming paradigm that emphasizes the use of pure functions to solve problems. It is a…

  • JavaScript: Mutable & Immutable

    JavaScript: Mutable & Immutable

    In JavaScript, objects can be mutable or immutable. A mutable object can be changed after it is created, while an…

  • JavaScript: Set

    JavaScript: Set

    The Set data type is a collection of unique values. It allows you to store values of any data type, such as numbers…

    1 条评论
  • JavaScript Prototype: Understanding Object Inheritance

    JavaScript Prototype: Understanding Object Inheritance

    When writing JavaScript code, you may come across the term "prototype". It's an important concept to understand, as it…

    1 条评论
  • JavaScript Dynamic Import

    JavaScript Dynamic Import

    If you're a JavaScript developer, you're probably familiar with the import statement that is used to load modules…

  • JavaScript: Iterators & Generators

    JavaScript: Iterators & Generators

    Iterators In JavaScript, an iterator is an object that provides a sequence of values, one at a time, when requested…

    1 条评论
  • JavaScript Currying

    JavaScript Currying

    JavaScript currying is a technique used to transform a function that takes multiple arguments into a sequence of…

    1 条评论
  • Space and Time Complexities

    Space and Time Complexities

    As a programmer, it's essential to understand the concepts of space and time complexity in your code. These concepts…

  • JavaScript best practices

    JavaScript best practices

    As JavaScript has become one of the most widely used programming languages, it is crucial to follow the best practices…

  • JavaScript Design Patterns: The Secret to Writing Maintainable Code

    JavaScript Design Patterns: The Secret to Writing Maintainable Code

    As a JavaScript developer, you know that writing clean, efficient, and maintainable code is essential. But with the…

社区洞察

其他会员也浏览了