300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > javascript遍历对象属性和方法

javascript遍历对象属性和方法

时间:2022-07-04 08:40:30

相关推荐

javascript遍历对象属性和方法

使用javascript遍历对象的属性和方法

<!DOCTYPE html><html xmlns="/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title></title><script src="lib/jquery-1.8.2.js"></script><script type="text/javascript">//对象function Programmer() {this.name = "李小牛";this.sex = "男";this.age = 25;this.work = proFun;}//方法function proFun() {$("#function").append("程序员的工作是写代码");}function foreachObj() {//声明对象var pro = new Programmer();//遍历对象属性for (var p in pro) {//判断是否为方法if (typeof (pro[p]) == "function") {//执行放阿飞pro[p]();} else {//打印属性$("#attribute").append(p + ":" + pro[p] + "\t");}}}</script></head><body><input type="button" value="执行对象方法" onclick="foreachObj()" /><div id="attribute"></div><div id="function"></div></body></html>

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。