开发者

js objects and properties

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);
}


for(var prop in myobject) {
    alert(prop);
    alert(myobject[prop]);
}


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.


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

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜