⬅ Go back to: Working with Properties

Private Properties

One of the most important principles of object oriented programming – delimiting internal interface from the external one.

That is “a must” practice in developing anything more complex than a “hello world” app.

To understand this, let’s break away from development and turn our eyes into the real world.

Usually, devices that we’re using are quite complex. But delimiting the internal interface from the external one allows to use them without problems.


Enumerating Properties

Using for in loop

Sometimes you need to iterate over or enumerate the properties of an object. We can use the for in loop.


    function Circle(radius) {
        this.radius = radius;
        this.draw = function() {
            console.log('draw');
        }
    }
    const circle = new  Circle(10);

    /**
     * Enumerate all the members in an object.
     * Use "for in" loop.
     */
    for (let key in circle) {        
        console.log(key); // access member's name

        // access a member's value (property or method)
        console.log(key, circle[key]); 

        // access a member's value (property only)
        if (typeof circle[key] !== 'function')
            console.log(key, circle[key]);
    } 

Output:

radius, draw

radius 10, draw function(){...}

radius 10


Using Object.keys method

There is another approach to get the keys in an object. We can use the Object.keys method then pass our object in it.

With this approach, we cannot separate properties from methods.


    function Circle(radius) {
        this.radius = radius;
        this.draw = function() {
            console.log('draw');
        }
    }
    const circle = new  Circle(10);
    
    /**
     * Enumerate all the keys in an object.
     * Use "Object.keys"
     */
        const keys = Object.keys(circle);
        console.log(keys);

Output: ["radius", "draw"]


Using in operator

To check if an object has a given property, we can use the in operator.


    function Circle(radius) {
        this.radius = radius;
        this.draw = function() {
            console.log('draw');
        }
    }
    const circle = new  Circle(10);
  
    /**
     * To check the existence of a property of a method in an object.
     * Use the "in" operator.
     */
        if ('radius' in circle)
        console.log('Circle has a radius.');

Output: Circle has a radius.


Abstraction

We can hide the details and the complexity then show only the essentials. We can also isolate the impact of changes.

Example of an abstraction

The goal is to hide the method to consumers and expose the essentials only.


    function Circle(radius) {
        this.radius = radius;
        
        // local variable or private property
        let defaultLocation = { x: 0, y: 0 };

        // local variable or private method
        let computeOptimumLocation = function(factor) {
            // ....
        }

        this.draw = function() {
            computeOptimumLocation(0.1)
            // this.radius - this is how you call members from this object.
            // defaultLocation - this is how you call private members from this object.

            console.log('draw');
        }
    }
    const circle = new  Circle(10);

In this example, defaultLocation and computeOptimumLocation are not accessible to consumers.

Name / Term Definition
Scope

It refers to the current context of code, which determines the accessibility of variables to JavaScript.

The two types of scope are local and global: Global variables are those declared outside of a block. Local variables are those declared inside of a block.

Closure

In contrast to scope, we have "closure". A closure determines what variables will be accessible to an inner function. An inner function can access local variables from itself and from its parent function.

It's the combination of a function bundled together (enclosed) with references to its surrounding state (the lexical environment). In other words, a closure gives you access to an outer function's scope from an inner function.


Conclusions

To enumerate all the members in an object, use for in loop.
To enumerate all the keys in an object, use Object.keys method.
To check the existence of a property of a method in an object, use the in operator.
Scope is temporary but Closure stays there.