Search code examples
javascripti2b2

What does this JavaScript mean?


I am working on a legacy application and all JS seems mystrious to me.
Here is the few mysterious lines which are loaded before all and I don't have any idea what they are doing.

var i2b2 = {sdx:{TypeControllers:{},Master:{_sysData:{}}},events:{},hive:{cfg:{},helpers:{},base_classes:{}},h:{}};  
if (undefined==i2b2.hive) { i2b2.hive = {}; }     
i2b2.hive.tempCellsList = [
        { code: "PM",
          forceLoading: true 
        },
        { code: "ONT"   },
        { code: "CRC"   },
        { code: "WORK"},
        { code: "SHRINE"},
        { code: "PLUGINMGR",
           forceLoading: true,
           forceConfigMsg: { params: [] }
        }
    ];

There are many more var and if statements but they are doing same thing with different variables.
Please help me to solve this mystery.


Solution

  • The first line initialises i2b2 using nested object literals.

    var obj = {}; is a shorter way of writing var obj = new Object();

    A simple object literal will be

    var simpleObject = {
        property1: "Hello",
        property2: "MmmMMm",
        property3: ["mmm", 2, 3, 6, "kkk"],
        method1: function() {
            alert("my method")
        }
    };
    

    A nested one will be

    var rectangle = {
        upperLeft: {
            x: 2,
            y: 2
        },
        lowerRight: {
            x: 4,
            y: 4
        }
    };
    

    Yours is a classic.

    var i2b2 = {
        sdx: {
            TypeControllers: {},
            Master: {
                _sysData: {}
            }
        },
        events: {},
        hive: {
            cfg: {},
            helpers: {},
            base_classes: {}
        },
        h: {}
    };
    

    The second line should be IMHO

    i2b2.hive = i2b2.hive || {};
    

    This just says that if hive is undefined create a new object.

    The last lines create a property tempCellsList to the object hive. ( Please note that hive in turn is a property of i2b2 ) Lastly a new array of objects are added to the property tempCellsList