Javascript object encapsulation

How do we achieve data encapsulation with Javascript objects?

Private member variables in objects.

  • All properties or let us say member variables defined with the "this" keyword inside a function object is of public type, which mean that they can be accessed from outside of a created object.
    function Person(n) {
    // name and getName ar both public
        this.name = n;
        this.getName = function() { return this.name; }
    }
  • All variables, which are defined with the "var" keyword and all the methods that a constructor function contains is private and therefore NOT accessible from outside of a created object. The same applies to all function arguments.
  • One of the most important things in object-oriented programming (OOP) is data encapsulation, which means to make private properties and then define public access methods to change these.
  • All public methods, which are defined inside a function object, have access to all defined private member variables and private methods in a created object.
  • Public methods defined on the outside of a function object can not access private member variables as they are not defined in the same scope as those.

    Example:
    <script type="text/javascript">
      function Rectangle(h,w) {
        var width=w; // Both the 'width' and 'w' is private
        var heigth=h; // Both the 'height' and 'h' is private
        this.setWidth= function(w){ width=w;}
        this.setHeight= function(h){ heigth=h;}
        this.getWidth= function(){ return width;}
        this.getHeight= function(){ return heigth;}
        this.constructor.prototype.getDiagonal=function() {
          return Math.sqrt(heigth*heigth+width*width);
        };
      }
      Rectangle.prototype.getArea=function() {
        // We must use accessors in a prototype kind of method,
        // then these methods can not access the private members
        // of a created object.
        return this.getWidth()*this.getHeight();
      };
      var rect = new Rectangle(60,70);
      rect.setHeight(20);
      document.write("The rectangle area is: "+rect.getArea()+"<br>");
      document.write("The rectangle diagonal is: "+rect.getDiagonal()+"<br>");
    </script>
  • Private methods have no directly access to properties that are defined to be public with "this" keyword in a function object. To achieve this, one can define a variable that has the same reference as "this" refers to.

    Example:
    <script type="text/javascript">
      function Person(n, y) {
        var name=n;
        var year=y;
        // Set a variable to the same as this
        var thisObj=this;
        this.setName= function(n){ name=n;}
        this.getName= function(){ return name;}
        this.setYear= function(y){ year=y;}
        this.getYear= function(){ return year;}
        var born = function() {
          var nYear=new Date().getFullYear();
          // Use the thisObj variable which is the same as this refer to.
          return nYear-thisObj.getYear();
          // The "this" keyword inside this function refers to an object
          // created by the constructor function of this function object.
        }
        this.getBornYear= function() {
          return born();
        }
      }
      var person = new Person("Nikita",60);
      document.write(person.getName()+" was born in "+person.getBornYear()+"<br>");
    </script>

© 2010 by Finnesand Data. All rights reserved.
This site aims to provide FREE programming training and technics.
Finnesand Data as site owner gives no warranty for the correctness in the pages or source codes.
The risk of using this web-site pages or any program codes from this website is entirely at the individual user.