枚举JavaScript对象的函数
枚举JavaScript对象的函数
发布时间:2016-12-30 来源:查字典编辑
摘要:From:JavaEye.com枚举JavaScript对象的函数:functioniterator(obj){for(varpropert...

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>

推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
相关阅读
网友关注
最新Javascript教程学习
热门Javascript教程学习
编程开发子分类