This article will introduce a method which enables encapsulation along with creating new Instance in Javascript at the same time.
In the previous article, I have introduced a way to write module Pattern in Javascript. Today, we are going to discuss encapsulation in Javascript. However, with a created module, it is only equivalent to class Singleton, which means that it is impossible to create another Object as Instance of that module.
http://blog.0x1115.org/2015/08/11/javascript/
In this article, I am going to introduce a method which enables encapsulation along with creating new Instance in Javascript at the same time.
Create Class
 Declare Properties and Methods
Overall, we can see that:
  • Private methods and properties should be exactly written in coding convention. (adding “_” in the beginning)
  • Distinguish public methods and private methods by adding ‘this’ before names of the methods. In fact, this means we are adding public methods in properties of Object as Instance of class Animal.
Example
When Private Methods or Properties are called, their values will be ‘undefined’, while Public Methods or Properties can be used as normal.
Duong Thanh Hai
Related posts: