February 10, 2015

How to traverse javascript object?

For in loop is one of my favourite commands of JavaScript and it just one step behind the modest alert() ;) 

It is a very powerful command and helps you to loop through any JavaScript object without knowing what object is all about. You don't need to know the types that exists in the object or number of child objects that object has. You just need have the handle of the object and you can loop through all its properties.

It is quite handy if you cant get your head around Siebel Open UI and you want to know what else is available in that object and how the functions are implemented in an object.

Examples:
MyPM.prototype.Setup = function (propSet) {       
    for(var x in propSet){console.log(x + "" + propSet[x]);
    }
        SiebelAppFacade.MyPM.superclass.Setup.call(this, propSet);
}

In this example I am trying to find what else is passed through the property set by Siebel to the Setup function of Presentation Model. and the following I am trying to all the methods that are available by theApplication() object.

for(var x in theApplication()){
console.log(x + " " + theApplication[x]);
}


For those who don't know, For in loop is just plain old JavaScript and is not some thing the jQuery offers.

Happy Hacking :)

No comments :

Post a Comment