Skip to content
the poor coder

function(global factory)

Poor Man
Published
Reading time
1 min read
JavaScript

    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:

JavaScript

    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:

JavaScript

    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.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC