js 继承2

105 阅读1分钟

call和apply继承

<script>
        // let obj1={
        //     a:'tom'
        // }
        // let obj2={
        //     a:'tom'
        // }
        // function f(){
        //     console.log('this指向',this);
        //     return this.a;
        // }
        // console.log(f.call(obj1));

        /* 构造函数绑定实现继承 */
        /* 在子类内部调用父类的方法 通过call()或apply()方法 */
        /* function Person(){
            this.foot=2
        }
        function Student(){
            // Person.call(this)
            Person.apply(this)
        }
        console.log( new Student() ); */

        function Car(gang, bsx) {
            this.gang = gang;
            this.bsx = bsx;
            this.lunzi = 4;
        }
        function Fn(name, price, gang, bsx) {
            Car.call(this, gang, bsx);
            // Car.apply(this,[gang,bsx]);
            this.name = name;
            this.price = price;
            this.print = function () {
                document.write(`${this.name}--${this.price}--
                ${this.lunzi}个轮子--${this.gang}缸--${this.bsx}变速箱`);
            }
        }
        let car1 = new Fn('奔驰', '100w', 4, 'cvt');
        // console.log(car1);
        console.log(new Fn());
        // console.log(new Car());
        car1.print();
    </script>

组合继承

<script>
        // function Person() {
        //     /* 实例属性 */
        //     this.head = 1;
        //     this.foot = 2;
        // }
        // /* 原型属性和方法 */
        // Person.prototype.weight='70kg';
        // Person.prototype.eat = function () {
        //     console.log('会吃饭');
        // }
        // function Student() {
        //     Person.call(this);
        // }
        // Student.prototype = new Person();
        // Student.prototype.constructor = Student;
        // let stu1 = new Student();
        // console.log(stu1);

        function Car() {
            this.lunzi = 4;
        }
        Car.prototype.run = function () {
            document.write(`${this.name}--${this.lunzi}个轮子--会跑赛道`);
        }
        function Bc(name) {
            Car.call(this);
            this.name = name;
        }
        Bc.prototype = new Car();
        Bc.prototype.constructor = Bc;
        let bc1 = new Bc('奔驰');
        console.log(bc1);
        bc1.run();
    </script>

拷贝继承

<script>
        // function Person(){}
        // Person.prototype.head=1;
        // Person.prototype.foot=2;
        // Person.prototype.eat=function(){
        //     document.write('eat eat eat');
        // }
        // function Student(){}
        function extend(child, parent) {
            for (var i in parent.prototype) {
                child.prototype[i] = parent.prototype[i];
            }
        }
        // extend(Student,Person);
        // let stu=new Student();
        // console.log(stu);
        // stu.eat()

        function Car() { }
        Car.prototype.lunzi = 4;
        Car.prototype.print = function () {
            document.write(`${this.name}--${this.lunzi}个轮子--会跑赛道`);
        }
        function Bc(name) {
            this.name = name;
        }
        extend(Bc, Car);
        let bc = new Bc('奔驰');
        bc.print();
    </script>