Js objects and properties

How can I list / encode all properties of an object? Knowing only the name of the object.

eg,

for(var prop in myobject){
 alert(prop.name);
 alert(prop.value);
}
+3
source share
3 answers
for(var prop in myobject) {
    alert(prop);
    alert(myobject[prop]);
}
+4
source

You are almost there!

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

Keep in mind that this will only jump to properties that do not have an attribute {DontEnum}. Almost all built-in properties and methods will not be repeated, you will see only custom properties and methods added either directly or through a prototype.

+2
source
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);
}

e

+1
source

Source: https://habr.com/ru/post/1748656/


All Articles