拷贝继承

56 阅读1分钟
    把父对象的所有属性和方法,拷贝进子对象
 将父对象的prototype对象中的属性,一一拷贝给Child对象的prototype对象
 -->
<script src="extend2.js"></script>
<script>
    // function Person() { }
    // Person.prototype.foot = 2;
    // Person.prototype.head = 1;
    // Person.prototype.say = function () {
    //     document.write('脚:' + this.foot + '头:' + this.head);
    // }
    // function Student() {
    //     this.name = 'zhangsan';
    // }
    // function extend2(child,parent) {
    //     let fu = parent.prototype;
    //     let zi = child.prototype;
    //     for (let key in fu) {
    //         zi[key] = fu[key];
    //     }
    // }
    // extend2(Student,Person)
    // let stu1 = new Student();
    // console.log(stu1);
    // stu1.say();

    function Car(){}
    Car.prototype.class = 'suv';
    Car.prototype.run = function (){
        document.write(`${this.class}${this.color}跑高速`);
    }
    /* let car1 = new Car();
    car1.run(); */
    function Bwm(color){
        this.color = color;
    }
    extend2(Bwm,Car);
    let bwm1 = new Bwm('黑色');
    console.log(bwm1);
    bwm1.run();
</script>