Hi Guys,

You can achieve encapsulation in JavaScript by making variables outside object prototype and able to get variable value with getter method. Similarly you can set value using setter method.

Information hiding is a common feature in other languages often as private and protected methods/properties. Even though you could simulate something like this on JavaScript, this is not a requirement to do Object Oriented programming


var BaseClass = function(){
  var name = "Shankar"
  
  this.getName = function(){
    return name;
  }
  
  this.setName = function(newName){
    name=newName;
  }
}

var obj = new BaseClass();
alert(obj.name); // undefined
alert(obj.getName()); // Shankar
obj.setName("Sameer");
alert(obj.getName()); // Sameer

Plunker: https://plnkr.co/edit/f1p3Mh6NZOhHjLwZBlkf?p=preview

Good Day :)