网络编程 
首页 > 网络编程 > 浏览文章

枚举JavaScript对象的函数

(编辑:jimmy 日期: 2025/5/11 浏览:3 次 )
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="/UploadFiles/2021-04-02/test.js"></head>
<body>
<script type="text/javascript">
 engineer = new Engineer();
 iterator(engineer);

</script>
</body>
</html>
上一篇:在Javascript中定义对象类别
下一篇:JavaScript使用prototype定义对象类型(转)[
一句话新闻
Windows上运行安卓你用过了吗
在去年的5月23日,借助Intel Bridge Technology以及Intel Celadon两项技术的驱动,Intel为PC用户带来了Android On Windows(AOW)平台,并携手国内软件公司腾讯共同推出了腾讯应用宝电脑版,将Windows与安卓两大生态进行了融合,PC的使用体验随即被带入到了一个全新的阶段。
友情链接:杰晶网络 DDR爱好者之家 南强小屋 黑松山资源网 白云城资源网 网站地图 SiteMap