来自:JavaEye.com
枚举JavaScript对象的函数:
function iterator(obj) {
for (var property in obj) {
document.writeln("
"属性 " : " obj[属性] "
");
}
}
一个简单的示例(test.js):
function Employee () {
this.name = "";
this.dept =“一般”;
}
function Manager() {
this.reports = [];
}
Manager.prototype = new Employee();
函数 WorkerBee() {
this.projects = [];
}
WorkerBee.prototype = new Employee();
function SalesPerson() {
this.dept = “销售”;
this.quota = 100;
}
SalesPerson.prototype = new WorkerBee();
函数工程师(){
this.dept = “工程”;
this.machine = “”;
}
Engineer.prototype = new WorkerBee();
Engineer.prototype.specialty = "代码";
函数迭代器(obj) {
for (var property in obj) {
document.writeln("
" property " : " obj[property] "
");
}
}
HTML 页面为:
JavaScript p {
字体大小:12px;
字体系列:Verdana; 行高:0.5em;
} 风格>
<script></script> <script> <BR> engineer = new Engineer(); <BR> iterator(engineer); <br><br></script>