In javascript, a variable can be defined using ‘var’ statement. One can define an ‘Object’ variable and associate attributes instantly, as shown in example below:
var oPerson = new Object();
oPerson.Name = 'Roshan Kumar';
oPerson.Age = 26;
oPerson.City = 'Mumbai';
Now, suppose I have such an object and I want to loop through all the attributes associated with ‘Object’ variable. (This could be required if you get such object as function parameter and you do not know the actual attributes associated with it.) Try following piece of code.
var attributeList = "";
for (var attribute in oPerson)
{
if(attribute == 'clone') continue;
attributeList += String(attribute) + ": " + eval('oPerson.' + String(attribute)) + "\n";
}
alert(attributeList);
I am not sure why an attribute with clone appears.
Monday, March 17, 2008
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment