JavaScript – Encapsulation


Encapsulation is one of the fundamental OOP concepts.

What is encapsulate meaning ?

Encapsulate means enclosing something in a capsule.

It is also termed as enclosing all the things and forming a closed container.

What is encapsulation in javascript ?

Encapsulation binds the data variables with the functions accessing that data.

This controls the data and validates it.

How to achieve encapsulation ?

First to make the data variables private, use “var” keyword.

Then the getter and setter method is used to get and set the data variables.

What is getter method in javascript ?

The getter method binds an object property to a function.

Moreover, that function will be called when that property is accessed.

Javascript coding basics !!!

Click to Learn More about – Javascript online learning

Syntax :

get prop() { statements }

get [expression]() { statements }

prop :


The name of the property to bind to the given function.

expression :

Expression for a computed property name to bind to the given function.

Rules and limitations for getter method :

It can have an identifier which is either a number or a string;

It must have exactly zero parameters.

Example :

<script>
                        var obj = {
                                values: ['x', 'y', 'z'],
                                get latest() {
                                        if (this.values.length == 0) {
                                                return undefined;
                                        }
                                        return this.values[this.values.length - 1];
                                }
                        }
                document.writeln("The latest val in values = " + obj.latest);
              </script>

Output :

How to remove the getter method in javascript ?

A getter can be removed using the delete operator.

Example :

<script>

delete obj.latest

</script>

What is the setter method in javascript ?

The setter method binds an object property to a function.

Moreover, when that function is called there attempts to set a property.

Syntax :

set prop(value) { statements }

set [expression](value) { statements }

prop :

The name of the property to bind to the given function.

expression :

Expression for a computed property name to bind to the given function.

value :

This is the value attempted to be assigned to prop.

Rules and limitations for setter method :

It can have an identifier which is either a number or a string;

It must have exactly one parameter.

Example :

<script>
                        var names = {
                                set current(name) {
                                        this.log.push(name);
                                },
                                log: []
                        }
                        names.current = 'Abdul';
                        names.current = 'Rahul';
                        names.current = 'Sachin';
                        names.current = 'Ganguly';
                        document.writeln("The latest val in values = " + names.log);
                </script>

Output :

Encapsulation allows us to use getter and setter methods in the following cases.

case 1 : Read and Write operation

Both getter and setter methods are used.

case 2 : Read operation


Only getter method is used.

case 3 : Write operation

Only the setter method is used.

Example :

<script>
                        class StudentInfo{
                                constructor(name,age,address){
                                        this.name = name;
                                        this.age = age;
                                        this.address = address;
                                }
                                getName(){
                                        return this.name;
                                }
                                getAge(){
                                        return this.age;
                                }
                                getAddress(){
                                        return this.address;
                                }
                                setName(name){
                                        this.name = name;
                                }
                                setAge(age){
                                        this.age = age;
                                }
                                setAddress(address){
                                        this.address = address;
                                }
                        }
                        var a = new StudentInfo("Abdul", "14","Chennai");
                        document.writeln("Name = " + a.name+"<br/>");
                        document.writeln("Age = " + a.age+"<br/>");
                        document.writeln("Address = " + a.address+"<br/><br/>");
                        document.writeln("<b>Getting StudentInfo by using Getter Method</b> <br/>");
                        document.writeln("Name = " + a.getName()+"<br/>");
                        document.writeln("Age = " + a.getAge()+"<br/>");
                        document.writeln("Address = " + a.getAddress()+"<br/><br/>");
                        document.writeln("<b>Getting New StudentInfo by using Setter and Getter Method</b> <br/>");
                        a.setName("Rahul");
                        a.setAge("12");
                        a.setAddress("Pune");
                        document.writeln("Name = " + a.getName()+"<br/>");
                        document.writeln("Age = " + a.getAge()+"<br/>");
                        document.writeln("Address = " + a.getAddress()+"<br/><br/>");
                </script>

Output :

Miscellaneous – Perfect Logo Design Option

Looking for the best logo design option for your website ?

One of the best option for Logo designing/making and to show the world the perfect identity of your business.

We strongly recommend you, one of the best option. Have a look at it.