Loading

Encapsulation

  • Encapsulation is the fundamental principle of object-oriented programming (OOP).
  • It helps to organize and protect the data and methods associated with an object. 

Example:

<!DOCTYPE html>
<html>


<body>


  <script>
    function Student(name, marks) {
      var s_name = name;
      var s_marks = marks;
      Object.defineProperty(this, "name", {
        get: function () {
          return s_name;
        },
        set: function (s_name) {
          this.s_name = s_name;
        }


      });


      Object.defineProperty(this, "marks", {
        get: function () {
          return s_marks;
        },
        set: function (s_marks) {
          this.s_marks = s_marks;
        }


      });


    }
    var stud = new Student("Ravi", 50);
    document.writeln(stud.name + " " + stud.marks);
  </script>


</body>


</html>

Output:

Ravi 50