js的六种继承(上)

本文详细解析JavaScript中六种常见的继承实现方法,包括原型链式、构造函数式、组合式继承等,探讨每种方式的优缺点及适用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

js的六种继承(上)

		// 父类
        function Father(name, age) {
            this.name = name;
            this.age = age;

            this.sayHello = function () {
                console.log("hello son")
            }
        }

        Father.prototype.sex = 'man';

        Father.prototype.sayHello2 = function () {
            console.log("hello my son")
        }

原型链式继承

	// 1.原型链式继承
        function Son() {
        }
        // 原型链式继承的关键就是把父类的实例,放到子类的原型上
        Son.prototype = new Father();

        function Son1() {
        }
        Son1.prototype = new Father();

		//以下是打印输出
        console.log("Son==>", new Son());
        console.log("Son.sex==>", new Son().sex);
        new Son().sayHello();
        new Son().sayHello2();

        console.log("Son1==>", new Son1());
        console.log("Son1.sex==>", new Son1().sex);
        new Son1().sayHello();
        new Son1().sayHello2();

        console.log("============华丽分割线==========")
        // 在这里我修改原型上的属性的话,两个实例都会改变
        Son1.sex = "woman";
        console.log("Son1.sex==>", new Son1().sex);
        console.log("Son.sex==>", new Son().sex);

在这里插入图片描述

原型链式继承总结
简单得说,原型链式继承就是把父类的实例对象放在子类的原型上
优点:能继承父类构造函数内部的方法,父类构造函数原型上的方法和属性
缺点:1.不能传参,缺少灵活性
	 2.继承的父类构造函数原型上的属性是共享的,一旦修改,一改全改

构造函数式继承

 		function Son(name, age) {
 			//构造函数式继承关键在于在子类内部,使用call或者apply引入父类构造函数
            Father.call(this, name, age)
        }
        
        function Son1(name, age) {
            Father.call(this, name, age);
            this.age = age;
        }
		
		 const son = new Son('小明', 18);
        const son1 = new Son1('小红', 22)

        console.log("son==>", son);
        console.log("son.name==>", son.name)
        console.log("son.age==>", son.age)
        console.log("son.sex==>", son.sex);
        son.sayHello();
        // son.sayHello2(); //会报错

        console.log("son1==>", son1);
        console.log("son1.name==>", son1.name)
        console.log("son1.age==>", son1.age)
        console.log("son1.sex==>", son1.sex);
        son1.sayHello();
        // new Son1().sayHello2();

在这里插入图片描述

构造函数式继承总结
构造函数式继承关键在于在子类内部,使用call或者apply引入父类构造函数
优点:1.能传递参数,继承更加灵活,
	 2.由于每一个都是一个新实例,属性不再是共享的
缺点:1.父类构造函数上的属性和方法都不能继承

组合式继承

通过观察可以发现,原型链式继承和构造函数式继承的优缺点是刚好相反的。
有人想可不可以两者一起综合起来,这就是组合式继承
        function Son(name, age) {
            Father.call(this, name, age)
        }
        Son.prototype = new Father();

        function Son1(name, age) {
            Father.call(this, name, age);
            this.age = age;
        }
        Son1.prototype = new Father();
		
		const son = new Son('小明', 18);
        const son1 = new Son1('小红', 22)

        console.log("son==>", son);
        console.log("son.name==>", son.name)
        console.log("son.age==>", son.age)
        console.log("son.sex==>", son.sex);
        son.sayHello();
        son.sayHello2();

        console.log("son1==>", son1);
        console.log("son1.name==>", son1.name)
        console.log("son1.age==>", son1.age)
        console.log("son1.sex==>", son1.sex);
        son1.sayHello();
        son1.sayHello2();
        
        son.sex = 'women'
        console.log("son.sex==>", son.sex);
        console.log("son1.sex==>", son1.sex);

在这里插入图片描述

组合式继承总结
优点:可继承父类原型上属性和方法,可传参
缺点:1.每一次new一个实例的时候,相对于父类被new了两次,会耗内存
凡是有关原型链式继承的,都会出来原型链紊乱的问题
		//打印一下子类原型的构造器
       console.log(Son.prototype.constructor)
       //从下图可以看到,构造器指向了父类构造函数,但是这是不符合我们的需要的

在这里插入图片描述

//纠正原型链 
 Son.prototype = new Father();
 //在把父类实例放入子类原型后,需要把原型构造器的指向强行指向本身
 Son.prototype.constructor = Son;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值