继承

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
<!--
1.什么是继承
比如:充值会员分为普通会员和vip会员,那么普通会员有的vip会员肯定有,vip会员有的普通会员不一定有,普通会员可以看出一个父级,vip会员可以看出一个子级,父级有的子级一定有,子级有的父级不一定有
2.如何编写继承

     -->
</head>
<body>
    <script>
        //封装继承
        function inherit(Child,Parent){
     //WsxString的原型指向=objectprototype   经过setprototypeof由指向的object原型指向到User,Vip原型上
            Object.setPrototypeOf(Child.prototype,Parent.prototype)
        }

           
        function User(username,password){
            this.name = username
            this.password = password
        }

        function VipUser(username,password,member){
            //username和password重复了,只保留一下
            User.call(this,username,password)
            this.member = member
        }

        User.prototype.wsxString = function(){
            console.log('可看免费视频')
        }

        VipUser.prototype.wsxString = function(){
            console.log('观看付费视频')
        }


        inherit(VipUser,User)
       
       var user = new User('12','34')
       console.log(user)
       user.wsxString()

       var VipUser = new VipUser('56','78','2024-02-13')
       console.log(VipUser)
       VipUser.wsxString();


    </script>
</body>
</html>

posted on 2023-04-05 22:35  爱前端的小魏  阅读(15)  评论(0编辑  收藏  举报

导航