tags:

views:

50

answers:

3

How Could I list/loop all properties of an object? Knowing only the object name.

eg

for(var prop in myobject){
 alert(prop.name);
 alert(prop.value);
}
+2  A: 

You're almost there!

for(var prop in myobject){
  alert(prop);           // -> property name
  alert(myobject[prop]); // -> property value
}

Be aware that this will only iterate over properties that don't have the {DontEnum} attribute. Almost all built-in properties and methods will not be iterated over, you will only see custom properties and methods added either directly or via the prototype.

Andy E
+3  A: 
for(var prop in myobject) {
    alert(prop);
    alert(myobject[prop]);
}
Darin Dimitrov
Remember to use `.hasOwnProperty`
Sean Kinsey
I was just tying to see google map marker properties. (api2)Works thanks, except there are many of properties!document.write might be better next time instead of alert. oops
Harry
+1  A: 
myobj.prototype.details= function(delim, sortfun){
    delim=delim || ', ';
    var list= [];
    for(var p in this){
        if(this.hasOwnProperty(p){
            list[list.length]=p+':'+this[p].toString();
        }
    }
    if(typeof sortfun==function) list.sort(sortfun);   
    return list.join(delim);
}

f

kennebec