zoukankan      html  css  js  c++  java
  • javascript遍历对象属性和方法

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

    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/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] + "	");
                    }
                }
            }
        </script>
    </head>
    <body>
        <input type="button" value="执行对象方法" onclick="foreachObj()" />
        <div id="attribute"></div>
        <div id="function"></div>
    </body>
    </html>
  • 相关阅读:
    c#将 1, 2, ..., 9共 9 个数字分成 3 组
    信息学院本科生创新项目总结
    Element-ui的使用
    fastmock接口管理
    mock安装与使用
    开闭原则
    里氏替换原则
    依赖倒置原则
    接口隔离原则
    单一职责原则
  • 原文地址:https://www.cnblogs.com/Mo-MaTure/p/5119992.html
Copyright © 2011-2022 走看看