Get up to 80 % extra points for free! More info:

Lesson 15 - Encapsulation In JavaScript

In the previous lesson, Static members in JavaScript, we explained static members. In today's JavaScript tutorial, we're going to look at encapsulation.

Encapsulation

Encapsulation is one of the cornerstones of the object-oriented programming philosophy. We also use the term visibility for this issue, which already indicates what the concept is about. Encapsulation allows us to hide the properties or methods of an object that we then can use from within the object but are no longer accessible from the outside. So we can't access them via the dot operator, but they can be access by various object methods.

The advantage of hiding properties and methods is to prevent errors. Programmers use a given object as a black box and only as it was designed. The internal code that other objects should not interfere with is then hidden.

Unfortunately, the encapsulation principle is still not fully available in JavaScript, as the language itself doesn't support it. All the ways to encapsulate object members are only hacks. Such methods have been invented by JavaScript programmers through time and you can encounter them in differently old codes. Certainly it makes no sense to show all these ways, which would require a separate course, but we'll go through only the latest ones. If you won't encapsulate your JavaScript objects, it won't be a big problem. However, it's at least a good idea to use underscores to indicate that certain properties and methods should not be used from the outside.

In 2015, two new data types were introduced with the ES6 standard:


 

...End of the preview...
Continue further

You will gain knowledge worth hundreds of thousands for a few crowns

You've come here and that's great! We believe that the first lessons showed you something new and useful
Do you want to continue the course? Go to the premium section.

Buy this course

Buy all currently available lessons with exercise submitting and other features for just $7.20
Current account balance $0
By buying this package, you'll have access to all 20 articles (20 lessons) in this course.

This article is licensed: Premium, by buying this article, you agree with the terms of use.

What will you get from us in the next lessons?
  • Unlimited and permanent access to individual lessons.
  • High quality IT knowledge.
  • Skills to help you get your dream and well-paid job.

Article description

Requested article covers this content:

In this JavaScript tutorial, we'll learn to create private object properties and methods and explain in more detail what encapsulation actually is.

You gain credits by supporting our network. This is done by sending a helpful amount of money to support the site, or by creating content for the network.

Article has been written for you by Štěpán Zavadil
Avatar
Activities