javaScript系列之使用构造方法创建js对象(八)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script type="text/javascript">

        function Student(){
            //下面abcd就是属性
            //this,指代的当前对象,this.a = ..就是将"举世无双"赋值给这个对象的a属性,
            // 如果a属性不存在,则赋给这个对象添加一个a属性,再赋值
            this.a = "举世无双";
            this.b = "一骑当千";
            this.c = "万夫莫敌";
            this.d = "所向披靡"

           //下面的laugh和eat就是方法
            this.laugh = function(){
                 document.getElementById("p1").innerHTML = "笑他";
            }

            this.eat = function(){
                alert("少吃点吧,该减肥了");
            }

            function song(){
                alert("一展歌喉")
            }
            song();
        }

        var stu = new Student();

        //使用prototype关键字扩展一个函数
        Student.prototype.goSchool = function(){
            alert("去上学");
        }


        //创建了第二个Student对象
        var  stu2 = new Student();


        function YYY(){
            this.y = "我是小y";
        }
        Student.prototype = new YYY();
        var stu3 = new Student;

    </script>
</head>
<body>

   <input type="button" value="a" onclick="alert(stu.a)">
   <input type="button" value="b" onclick="alert(stu.b)">
   <input type="button" value="c" onclick="alert(stu.c)">
   <input type="button" value="d" onclick="alert(stu.d)">
   <input type="button" value="laugh" onclick="stu.laugh()">
   <input type="button" value="eat" onclick="stu.eat()">
   <input type="button" value="song" onclick="Student">
   <input type="button" value="扩展去上学" onclick="stu2.goSchool()">
   <input type="button" value="继承" onclick="alert(stu3.y)">

   <p id="p1"></p>

</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值