Skip to main content
Engineering LibreTexts

5.7: A JavaScript Object Model that Includes Encapsulation and Data Hiding

  • Page ID
    29149
  • The problem with the object model in the previous section is that the constructed object was returned to the main program where all of its properties were visible. This is why the principal of encapsulation is broken. The easiest way to prevent the leaking of the object is to make the object be defined not by this, but by a function variable. Functions variables are only scoped as visible in the function itself, and this in effect makes the object itself private.

    To implement this new object model, the strategy from the last section needs to be tweaked a little. First, the Constructor Function will not construct a this object, but will create a function local variable and construct the object inside of this local variable. Now that the object is a local variable that cannot be accessed outside of the Constructor Function (which is the outer function), inner functions need to be defined that can access the properties of the object.

    Note that the object will still want to use prototype functions so that only one instance of the function needs to be defined, but these prototype functions must now be moved so they are inner functions of the Constructor function. This gives the prototype functions access to the variables defined in the outer function.

    The strategy for creating this type of object is as follows:

    1. The Constructor Function is defined, and it will take one argument, which is an options object of properties to be included and set in this object.
    2. A function local variable, named __properties, is defined of type object.
    3. The __cfName variable is assigned to the name of the Constructor Function in the __properties object. This is so that object can later be reconstructed if it loses its constructor (e.g. it is written and then read from a JSON data source).
    4. The property default values will be defined in the __properties object.
    5. All properties passed into the Constructor Function will be moved to properties in the newly constructed __properties object. The properties will include all properties in the options object, including those that do not have default values. The reason for this will be explained in the section on Unstructured Data later in this chapter.
    6. All methods that will act on this object will be included in the prototype object for this Constructor Function. These prototype functions will be defined inside of the Constructor Function, so they have access to the function local variable.
    7. A stringify method needs to be defined to allow the internal object (the __properties) object can be accessed and converted into a string.

    An example of implementing and using this definition for a Map object is presented below.

     Program 105 - Object example with encapsulation and data hiding
     
    <script>
    function Map(options) {
        // Set Defaults
            let __properties = new Object;
            __properties.title = "Please change the title"
            __properties.recenter = true;
            __properties.center = [-77, 32];
            __properties.__cfName = "Map"
    
    // Get properties from parameter
            outerloop:
        for (i in options) {
            for (j in __properties)   {
                if (i == j) {
                    __properties[i]= options[j];
                    continue outerloop;    
                }
            }
            console.log("Property " + i + " is not a default Map property");
            __properties[i] = options[i];
        }
        
        // Set accessor function
        Map.prototype.setTitle = function (title) {
            __properties.title = title;
        }
        Map.prototype.getTitle = function () {
            return __properties.title;                    
        }
        Map.prototype.setRecenter = function (recenter) {
            __properties.recenter = recenter;
        }
        Map.prototype.getRecenter = function () {
            return __properties.recenter;
        }
        
        Map.prototype.setCenter = function (center) {
            __properties.center = center;
        }
        Map.prototype.getCenter = function () {
            return __properties.center;
        }
        
        Map.prototype.stringify = function() {
            return JSON.stringify(__properties);                
        }
        
        Map.prototype.toString = function() {
            return ("title: " + __properties.title
                + " recenter: " + __properties.recenter
                + " center: " + __properties.center);
        }
    } //End of Constructor Function
    
    function getObjectFromJSON(string) {
        let parsedObject = JSON.parse(string);
        let cf = parsedObject["__cfName"];
        return new window[cf](parsedObject);
    }
    
    let m1 = new Map({title: "newMap"})
    let m2 = getObjectFromJSON(m1.stringify());
    console.log(m2.toString());
    
    </script>