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="test.js"></script> </head> <body> <script type="text/javascript"> engineer = new Engineer(); iterator(engineer); </script> </body> </html>
推荐阅读
js中几种去掉字串左右空格的方法
//recon 的思路: //------------- //去掉字串左边的空格 function ltrim(str) { if (str.charat(0) == " ") { //如果字串左边第一个字符为空格 str = str.slice(1);//将空格从字串中去掉 //这一句也可改成 >>>详细阅读
本文标题:枚举JavaScript对象的函数
地址:http://www.17bianji.com/kaifa2/JS/31221.html
1/2 1