Skip to content

Latest commit

 

History

History
68 lines (48 loc) · 2.44 KB

57-Encapsulation.md

File metadata and controls

68 lines (48 loc) · 2.44 KB

Expalin Encapsulation: Protected Properties and Methods

Encapsulation in JavaScript refers to the practice of binding data and functions within a single unit, called a class or object, and restricting access to them from outside.

In terms of protected properties and methods, encapsulation allows you to define properties and methods as "protected," meaning they can only be accessed from within the class or object, or from derived classes.

To create protected properties in JavaScript, you can use the underscore (_) prefix to indicate that a property should not be accessed outside the class or object. For example:

class MyClass {
  constructor() {
    this._myProtectedProperty = 'some value';
  }
}

To create protected methods, you can either use the same underscore prefix or define the method inside the class using the # symbol. For example:

class MyClass {
  _myProtectedMethod() {
    // some code
  }
  
  #myOtherProtectedMethod() {
    // some other code
  }
}

By using encapsulation and defining properties and methods as protected, you can help ensure that your code is more secure, maintainable, and less prone to errors caused by unintended external access.

Explain Encapsulation: Private Class Fields and Methods

Encapsulation refers to the principle of wrapping data and functions into a single unit, such as a class, in order to control access to that data and functions from outside the class. Private class fields and methods are a way to implement encapsulation in JavaScript.

Private class fields are class properties that can only be accessed or modified from within the class itself, and not from outside it. They are marked with the hash symbol (#) before their name:

class MyClass {
  #privateField = "private";

  publicMethod() {
    console.log(this.#privateField);
  }
}

In this example, #privateField is a private field that can only be accessed from within the MyClass class.

Private class methods are similar, but they are methods that can only be called from within the class itself:

class MyClass {
  #privateMethod() {
    console.log("private method");
  }

  publicMethod() {
    this.#privateMethod();
  }
}

In this example, #privateMethod is a private method that can only be called from within the MyClass class.

Using private class fields and methods helps to prevent unintentional modification of class data and ensures that certain functionality is only available to the class itself.