实现多重继承

225 阅读1分钟
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        function PersonA(){
            this.dao = 'dao';
        }
        function PersonB(){
            this.jd = 'jd';
        }
        function PersonC(){
            this.ff = 'ff';
        }
        function Person(){

        }

        function extend(o,...arr){
            if(arr.length == 0){
                return o;
            }
            o.prototype = new arr[0]();
            let p = o.prototype;
            for(let i = 0;i < arr.length - 1;++i){
                p.__proto__ = new arr[i + 1]();
                p = p.__proto__;
            } 
        }

        extend(Person,PersonA,PersonB,PersonC);

        let aa = new Person();

        console.log(aa.dao,aa.jd,aa.ff);
        
    </script>
</body>
</html>