开发者

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.


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


This javascript code creates a variable called ib2b that has a number of properties: sdx, events, hive, etc. Those properties hold more composite objects, which are constructed below.

The idea is that this global object can be referenced from other javascript code and it stores global configuration for the client-side application.


I'm not quite sure, what exactly you don't understand. There are two "strange" points about the code above, which I'll try to explain, but if that's not enough you will need to describe better what you don't understand:

  1. The code checks is i2b2.hive is is undefined and set it as an empty object, if it is. Since the property is obviously set in the previous line, my guess is that this code is generated dynamically and some of the logic (such as this check) is defined in the JavaScript code even if it could (should?) be the the server side code.

  2. undefined==i2b2.hive is a bad/wrong way to test "undefinedness", because undefined is not a reserved word in JavaScript.This just works, because undefined is just a variable that - by chance - happens to be undefined. Instead one should use if (typeof i2b2.hive == "undefined") ... or just if (i2b2.hive) ....


It seems like it's setting a few parameters in that i2b2 object. It doesn't "do" anything in itself, but it seems to set a few basic configuration settings for further execution. Try and look for similar occurrences in the code further below.

E.g it sets i2b2.hive.tempCellList[5].forceLoading to true. Later on the application probably has if-conditions, such as

for(var i in i2b2.hive.tempCellList)
{
    if(i2b2.hive.tempCellList[i].forceLoading === true)
    {
        // do something...
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜