枚举JavaScript对象的函数
时间:2021-07-13 08:22:38|栏目: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对象的函数:
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>
上一篇:array.prototype.silce.call 理解分析
栏 目:JavaScript代码
本文标题:枚举JavaScript对象的函数
本文地址:http://www.codeinn.net/misctech/156702.html