Home > Web Front-end > JS Tutorial > Functions to enumerate JavaScript objects_javascript tips

Functions to enumerate JavaScript objects_javascript tips

WBOY
Release: 2016-05-16 19:22:45
Original
1251 people have browsed it

From: JavaEye.com

枚举JavaScript对象的函数:
function iterator(obj) {
 for (var property in obj) {
 document.writeln("

"   property   " : "   obj[property]   "

");
 }
}

一个简单示例(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("

"   property   " : "   obj[property]   "

");
 }
}

HTML页面为:



JavaScript
p {
 font-size: 12px;
 font-family: Verdana;  line-height: 0.5em;
}


<script></script> <script> <BR> engineer = new Engineer(); <BR> iterator(engineer); <br><br></script>
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template