当前位置:编程学习 > JS >>

枚举JavaScript对象的函数

答案: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" />
<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>

上一个:Javascript实例教程(19) 使用HoTMetal(2)
下一个:取得input元素中部分选中(selected)的值

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,