枚举JavaScript对象的函数:
functioniterator(obj){
for(varpropertyinobj){
document.writeln("
"+property+":"+obj[property]+"
");}
}
一个简单示例(test.js):
functionEmployee(){
this.name="";
this.dept="general";
}
functionManager(){
this.reports=[];
}
Manager.prototype=newEmployee();
functionWorkerBee(){
this.projects=[];
}
WorkerBee.prototype=newEmployee();
functionSalesPerson(){
this.dept="sales";
this.quota=100;
}
SalesPerson.prototype=newWorkerBee();
functionEngineer(){
this.dept="engineering";
this.machine="";
}
Engineer.prototype=newWorkerBee();
Engineer.prototype.specialty="code";
functioniterator(obj){
for(varpropertyinobj){
document.writeln("
"+property+":"+obj[property]+"
");}
}
HTML页面为:
p{
font-size:12px;
font-family:Verdana;
line-height:0.5em;
}
engineer=newEngineer();
iterator(engineer);