【 tulaoshi.com - Web开发 】
From: JavaEye.com
枚举JavaScript对象的函数:
function iterator(obj) {
for (var property in obj) {
document.writeln("p" + property + " : " + obj[property] + "/p");
}
}
一个简单示例(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");
}
}
HTML页面为:
html
head
meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /
titleJavaScript/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