function(global factory)


    function globalFactory(obj) {
        // Create deep copy of an object
        let copy = Object.assign({}, obj);
        // Iterate over all properties of an object and make them global
        for (let key in copy) {
            window[key] = copy[key];
        }
    }

The above function is used to create a global namespace that can be accessed from anywhere in the application. The function takes an object as parameter and creates a deep copy of it. It then iterates over each and every property of the object and creates a global variable for it. This way the global namespace can be accessed and modified from anywhere in the application. For example, say we have an object named color that contains the following:


    const color = {
        primary: "#336699",
        secondary: "#FF0000"
    }

If we want to make the properties of this object accessible globally, we can call the globalFactory function as follows:


    globalFactory(color);

This would create two global variables, primary and secondary, that can be accessed from anywhere in the application. This way, the same object can be accessed and modified from various parts of the application instead of passing it as an argument to each function.

Subscribe to The Poor Coder | Algorithm Solutions

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe