From: JavaEye.com
Functions that enumerate JavaScript objects:
Function iterator (obj ){
For (var property in obj ){
Document. writeln ("<p>" + property + ":" + obj [property] + "</p> ");
}
}
A simple example (test. js ):
Function Employee (){
This. name = "";
This. dept = "general ";
}
Function Manager (){
This. reports = [];
}
Manager. prototype = new Employee ();
Function WorkerBee (){
This. projects = [];
}
WorkerBee. prototype = new Employee ();
Function SalesPerson (){
This. dept = "sales ";
This. quota = 100;
}
SalesPerson. prototype = new WorkerBee ();
Function Engineer (){
This. dept = "engineering ";
This. machine = "";
}
Engineer. prototype = new WorkerBee ();
Engineer. prototype. specialty = "code ";
Function iterator (obj ){
For (var property in obj ){
Document. writeln ("<p>" + property + ":" + obj [property] + "</p> ");
}
}
The HTML page is:
<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> JavaScript </title>
<Style type = "text/css">
P {
Font-size: 12px;
Font-family: Verdana;
Line-height: 0.5em;
}
</Style>
<Script language = "javascript" type = "text/javascript" src = "test. js"> </script>
</Head>
<Body>
<Script type = "text/javascript">
Engineer = new Engineer ();
Iterator (engineer );
</Script>
</Body>
</Html>