枚举JavaScript对象的函数

      2019-10-29 23:14      编程语言
From:JavaEye.com

枚举JavaScript对象的函数:
functioniterator(obj){
for(varpropertyinobj){
document.writeln("<p>"+property+":"+obj[property]+"</p>");
}
}

一个简单示例(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("<p>"+property+":"+obj[property]+"</p>");
}
}

HTML页面为:
<html>
<head>
<metahttp-equiv="Content-Type"content="text/html;charset=UTF-8"/>
<title>JavaScript</title>
<styletype="text/css">
p{
font-size:12px;
font-family:Verdana;
line-height:0.5em;
}
</style>
<scriptlanguage="javascript"type="text/javascript"src="test.js"></script>
</head>
<body>
<scripttype="text/javascript">
engineer=newEngineer();
iterator(engineer);

</script>
</body>
</html>