zoukankan      html  css  js  c++  java
  • JS_dom_自定义对象

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>自定义对象</title>
    <script type="text/javascript">
    //1.采用直接量方式创建对象
    function f1() {
    var student=
    {"name":"zs","age":"23","work":function(){
    alert("我学Java");
    }
    }
    alert(student.name);
    alert(student.age);
    student.work();
    }
    //2.构造器(new的函数/首字母大写)

    //1.内置构造器
    function f2() {
    var teacher= new Object();
    teacher.name="唐长老";
    teacher.age=18;
    teacher.work=function(){
    alert("我要取经");
    }
    alert(teacher.name);
    alert(teacher.age);
    teacher.work();
    }
    //2.自定义构造器
    function Coder(name,age,work){
    this.name=name;
    this.age=age;
    //this 指代当前对象
    //.job是给此对象增加job属性
    //=work是将参数work赋值给此属性
    this.job=work;
    }
    function f3() {
    var coder= new Coder("lisi",25,
    function(){alert("我学习Java");
    });
    alert(coder.name);
    alert(coder.age);
    coder.job();
    }
    </script>
    </head>
    <body>
    <input type="button" value="按钮1" onclick="f1();">
    <input type="button" value="按钮2" onclick="f2();">
    <input type="button" value="按钮3" onclick="f3();">
    </body>
    </html>

  • 相关阅读:
    表连接问题
    public interface Serializable?标记/标签接口
    4.21
    第十周周记
    测试
    第九周周记
    第七周周记
    fighting.
    fighting
    作业一
  • 原文地址:https://www.cnblogs.com/Bighua123/p/7639936.html
Copyright © 2011-2022 走看看