There are four core principles in object-oriented programming. Without them programming language can’t be called object-oriented. These principles are encapsulation, inheritance, polymorphism and abstraction. In this article, you will learn about these principles, their meaning, and how to use them.

Encapsulation

The first of the four core principles in object-oriented programming is encapsulation. The idea of encapsulation is that implementation details should not be visible to end users. For example, let’s say you have a class. Implementing the principle of encapsulation would mean that all properties of this class are private, hidden from other classes.

The only way to access these class properties would be through public accessor methods of that class. Accessor method is a method created for the purpose of accessing specific class property. This practice of hiding information or data about implementation is called “data hiding”.

To implement encapsulation in JavaScript, we create new class. Inside it, we declare two new properties, also called fields and members. We make all of them private. This will ensure all these properties are hidden. They will be inaccessible from the outside of the class. From now, the only way to access them is through methods inside that class.

This is the next thing we will do. We will create public setter and getter methods for each private property. These methods will allow us to view and modify values of these properties.

class User {
  // Create private class properties/fields
  // NOTE: Private fields was added to JavaScript in ES2015
  #_username
  #_email
  // Create getter method
  // to get username property
  get username() {
    return this.#_username
  }
  // Create setter method
  // to set, or change, username property
  set username(newUsername) {
    if (newUsername && newUsername.length === 0) {
      throw new Error('username must contain more than 0 characters.')
    }
    this.#_username = newUsername
  }
  // Create getter method
  // to get email property
  get email() {
    return this.#_email
  }
  // Create setter method
  // to set, or change, email property
  set email(newEmail) {
    if (newEmail && newEmail.length === 0) {
      throw new Error('email must contain more than 0 characters.')
    }
    this.#_email = newEmail
  }
}
// Create new instance of User class
let bob = new User()
// Set username
// This invokes username setter method
bob.username = 'bobby'
// Set email
// This invokes email setter method
bob.email = 'bobby@email.com'
// Access username
// This invokes username getter method
console.log(bob.username)
// 'bobby'
// Access username
// This invokes email getter method
console.log(bob.email)
// 'bobby@email.com'

In the example above, you have a class with two private properties. These properties are username and email. Next, you have one getter and one setter method for each of these properties. Getter method starts with keyword get and setter with keyword set. When you try to access one of these properties specific getter method is invoked…

#javascript #development #coding

4 Core Principles of Object-oriented Programming in JavaScript
36.30 GEEK