JS interview in 2 minutes / Encapsulation (OOP)
Tags: javascript, typescript, beginners, career
Published: 2021-05-03 09:51:56

---
title: JS interview in 2 minutes / Encapsulation (OOP)
published: true
description: Let's try to figure out how to answer common JS questions in an interview.
tags: javascript, typescript, beginners, career
cover_image: https://upload.wikimedia.org/wikipedia/commons/thumb/9/99/Unofficial_JavaScript_logo_2.svg/1920px-Unofficial_JavaScript_logo_2.svg.png
---

**Question:**
What is Encapsulation?

**Quick answer:**
Encapsulation can be used in any meaning of any of these terms or both:
* Mechanism used to restrict access to some of an object's properties.
* Mechanism that allows data access only via related methods or functions (setters and getters).

**Longer answer:**
Basically, the first definition is only about creating private properties.

Btw there are no private properties in JavaScript but looks like there is [a proposal](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/Private_class_fields). In Typescript private properties are present.

UPD: as @scott_yeatts has mentioned in the comments, it is still possible to use private properties in JavaScript via [The Revealing Module Pattern](https://www.oreilly.com/library/view/learning-javascript-design/9781449334840/ch09s03.html).

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wmdf8n40xbgkxz4pdew7.png)
 
Another definition is just a way to say that access to every property is implemented via getters and setters.

```typescript
class User {
  private salary = 0;

  setSalary(salary: number) {
    this.salary = salary
  }

  getSalary() {
    return this.salary
  }
}

let user = new User()
console.log(user.getSalary()) // 0
user.setSalary(10)
console.log(user.getSalary()) // 10
```

**Real-life applications:**
This getters & setters pattern was always confusing to me and felt like a total redundancy, haven't changed my opinion still. 

There is [a list](https://stackoverflow.com/questions/1568091/why-use-getters-and-setters-accessors) with a bunch of good reasons why getters & setters can be useful, but I can't say that I actually had an issue with any of the described topics.

// Here goes super opinionated section

Feels like the whole industry is moving to not deeply using OOP and preferring immutable objects and pure functions over data mutations, so this topic maybe just a tribute to old times 🤷

// end of super opinionated section

If you know a good example of beneficial use of getters and setters, please share it in the comments 🙏

**Resources:**
[wiki/encapsulation](https://en.wikipedia.org/wiki/Encapsulation_(computer_programming))
[tutorialspoint/java/encapsulation](https://www.tutorialspoint.com/java/java_encapsulation.htm)

**Other posts:**
* [JS interview in 2 minutes / Polymorphism (OOP)](https://dev.to/kozlovzxc/js-interview-in-2-minutes-polymorphism-oop-3pla)
* [JS interview in 2 minutes / Inheritance in OOP](https://dev.to/kozlovzxc/js-interview-in-2-minutes-inheritance-in-oop-53h2)
* [JS interview in 2 minutes / Object-Oriented Programming (OOP)](https://dev.to/kozlovzxc/js-interview-in-2-minutes-object-oriented-programming-oop-3443)

---

Btw, I will post more fun stuff here and on [Twitter](https://twitter.com/kozlovzxc). Let's be friends 👋