JS继承核心(1)第一种方式

2020-10-28 10:38:04 浏览数 (1)

代码语言:javascript复制
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <script type="text/javascript">
        function Person()//父类
        {
            this.name=null;
            this.age=0;
            this.say=function()
            {
                console.log(this.name, this.age);
            }
        }
        function Student()
        {
            this.score=0;
            this.study=function()
            {
                console.log("day day up");
            }
        }
       Student.prototype=new Person();//既然总结没有,就去原型里面找,把Person的实例对象赋值给student的原型对象,实例对象有的东西
        Student.prototype.constructor=Student;//为了不破坏关系,这样(构造函数)
         let stu = new Student();
        stu.name = "zs";
        stu.age = 18;
        stu.score = 99;
        stu.say();
        stu.study();
    </script>
</body>
</html>

0 人点赞