欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  web前端

枚举JavaScript对象的函数_javascript技巧

程序员文章站 2022-05-07 10:38:31
...
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