This is the 11th day of my participation in the August More text Challenge. For details, see: August More Text Challenge

Modularity has been a hot topic in recent years. To understand modularity, we must first understand private variables and private functions. Let’s talk about private variables first.

Private variables

Any variables defined in a function block can be considered private, since they cannot be accessed from outside the function or block. Private variables include function arguments, local variables, and other functions defined within the function.

    function add(x,y){
        let sum = x + y;
        return sum;
    }

Copy the code

Sum, x, and y are private variables that can only be used inside the function, not outside it. If the function creates a closure, then the closure can access the external three variables through the scope chain, thus creating a public method that accesses the private variable.

Privilege method

Privileged methods are common methods that can access a function’s private variables and private functions.

    function Person(name) {
        this.getName = function () {
            return name;
        };
        this.setName = function (value) {
            name = value;
        };
    }
    let person = new Person('jackson');
    console.log(person.getName()); // 'jackson' 
    person.setName('bear');
    console.log(person.getName()); // 'bear' 
Copy the code

This code defines two privileged methods getName and setName, each of which can be called outside the constructor and read and write the private name variable through them. But with this approach, the new method is created again for each instance, and we can avoid this problem by using static private variables.

Static private variable

Privileged methods can also be implemented by defining private variables and functions using private scope

      (function () {
        // Private variables and private functions
        let sum = 1;

        function private() {
            return false;
        }
        // The constructor
        MyObject = function () {};
        // Public and privileged methods
        MyObject.prototype.publicMethod = function () {
            sum++;
            returnprivate(); }; }) ();Copy the code

Here the anonymous function expression creates a private scope that contains the constructor and other methods. The first functions that are defined are private variables and private functions, and any functions that define the constructor and common methods. Public methods are defined on a constructor stereotype, which is similar to the typical stereotype pattern.

The module pattern

The module pattern is an extension of a singleton object that associates private variables and privileged methods through a chain of scope. Singletons are created using literals. A singleton is an object that has only one instance. Here is an example of a singleton.

    let singleton = {
        name: value,
        method() {
            // The code for the method}};Copy the code

In modular mode, a singleton, as a module, can be initialized to contain some private data that can be accessed through the public methods it exposes. Every singleton created in this way is an instance of Object, because the final singleton is represented by an Object literal. This doesn’t matter, though, because singletons are usually globally accessible and not passed as arguments to functions. The boilerplate code for the module pattern is as follows.

    let application = function () {
        // Private variables and private functions
        let components = new Array(a);/ / initialization
        components.push(new BaseComponent());
        // Public interface
        return {
            getComponentCount() {
                return components.length;
            },
            registerComponent(component) {
                if (typeof component == 'object') { components.push(component); }}}; } ();Copy the code

Module enhancement mode

We can choose the module enhancement mode if we encounter situations where a singleton object requires an instance of a particular type, but we must add additional properties or methods to it.

    let singleton = function () {
        // Private variables and private functions
        let privateVariable = 10;

        function privateFunction() {
            return false;
        }
        // Create an object
        let object = new CustomType();
        // Add privileges/public properties and methods
        object.publicProperty = true;
        object.publicMethod = function () {
            privateVariable++;
            return privateFunction();
        };
        // Return the object
        returnobject; } ();Copy the code