首页 > 其他分享 >JS的6中继承方式

JS的6中继承方式

时间:2023-08-19 20:46:43浏览次数:35  
标签:function console name 方式 继承 JS child 父类 log

1. 原型链继承

将父类实例作为子类的原型,这种方式下,子类实例可以共享父类实例的属性和方法,但是无法向父类构造函数传递参数。

    function Fun1() {         this.name = '我是名称'         this.getName = () => {             console.log(this.name)         }     }     Fun1.prototype.get = () => {         console.log('Fun1.prototype上的方法')     }     function Fun2() {}     Fun2.prototype = new Fun1()       const f1 = new Fun2()     console.log(f1.name)     f1.getName()     f1.get()

 

2. 构造函数继承

构造函数继承是指在子类构造函数中调用父类构造函数,并使用 call 或 apply 方法将父类的 this 指向子类实例。这种方式的缺点是无法继承父类原型上的方法。

function Parent(name) {
 	this.name = name;
 }

 Parent.prototype.say = function() {
	 console.log('父亲说:你要听话。')
 }

 function Child(name, age) {
 	Parent.call(this, name);
 	this.age = age;
 }

 const child = new Child('小红', 18);
 console.log(child.name); // 小红
 console.log(child.age); // 18
 child.say() // 报错 child.say is not a function

3. 组合继承

组合继承是指将原型链继承和借用构造函数继承结合起来。这种方式可以继承父类实例和原型上的属性和方法,但是会调用两次父类构造函数,且父类原型上的属性和方法会被继承两次。

function Parent(name) {
	 this.name = name;
 }

 Parent.prototype.sayHello = function() {
	 console.log('hello from parent');
 }

 function Child(name, age) {
	 Parent.call(this, name);
	 this.age = age;
 }

 Child.prototype = new Parent();
 Child.prototype.constructor = Child;

 const child = new Child('小红', 18);
 console.log(child.name); // child
 console.log(child.age); // 18
 child.sayHello(); // hello from parent

4. 原型式继承

原型式继承是指创建一个空对象,并将父类实例作为该空对象的原型。这种方式的缺点与原型链继承相同。

function createObject(obj) {
	 function F() {}
	 F.prototype = obj;
	 return new F();
 }

 const parent = {
 	 name: 'parent',
 	 sayHello: function() {
     	 console.log('hello from parent');
     }
 };

 const child = createObject(parent);
 console.log(child.name); // parent
 child.sayHello(); // hello from parent

5. 寄生式继承

寄生式继承是指创建一个新对象,并在该对象上增加一些父类的属性和方法,然后返回该对象。这种方式的缺点与原型式继承相同。

function createObject(obj) {
	 const o = Object.create(obj);
	 o.sayHello = function() {
	 	 console.log('hello from child');
	 };
	 return o;
 }

 const parent = {
	 name: 'parent'
 }

 const child = createObject(parent);
 console.log(child.name); // parent
 child.sayHello(); // hello from child

6. 寄生组合式继承

寄生组合式继承是指使用“借用构造函数”继承父类实例的属性和方法,并将子类原型指向一个父类实例的副本。这种方式可以避免调用两次父类构造函数,且不会继承父类原型上的属性和方法。

 function Parent(name) {
 	 this.name = name;
 }

 Parent.prototype.sayHello = function() {
	 console.log('hello from parent');
 };

 function Child(name, age) {
	 Parent.call(this, name);
	 this.age = age;
 }

 Child.prototype = Object.create(Parent.prototype);
 Child.prototype.constructor = Child;

 const child = new Child('小红', 18);
 console.log(child.name); // 小红
 console.log(child.age); // 18
 child.sayHello(); // hello from parent

 

7. ES6 中,可以使用 class 和 extends 关键字来实现继承。即定一个父类(也称为基类)和一个子类(也称为派生类),并通过 extends 关键字让子类继承父类的属性和方法。

 class Parent {
	 constructor(name) {
		 this.name = name;
     }
     sleep() {
		 console.log(`${this.name} 在睡觉`);
	 }
 }

 class Child extends Parent {
	 constructor(name) {
 		 super(name);
     }
     
     // 子类定义了一个新方法
     play() {
     		console.log('玩耍');
     }

     // 子类重写父类的 sleep 方法
     sleep() {
          super.sleep();
          console.log('睡觉');
	 }
 } 

 const child = new Child('小红');
 console.log(child.name); // 小红
 child.play(); // 玩耍
 child.sleep(); //  依次输出 “小红在睡觉” 和 “睡觉”

  

标签:function,console,name,方式,继承,JS,child,父类,log
From: https://www.cnblogs.com/qiuchuanji/p/17643076.html

相关文章

  • 卓钥商学苑:创业期间被合伙人背叛了怎么办?处理方式有哪些?
    来源:BV1bh411P7ef合伙人背叛了你怎么办?处理方式有哪些?当您选择和其他人一起创业,您会很快发现,与人合作还是很容易出现问题的。有些人在面对困难时可能会逃避,甚至会背叛您。这不仅会对您的事业产生负面影响。而且会给您带来巨大的心理压力和痛苦。那么当您的合作伙伴背叛您时,您......
  • vue.js:5108 [Vue warn]: Cannot find element: #body_container
    1、原因:我把Vue挂载元素的JS放在了html加载完成的前面了2、解决:放到html加载完成之后就可以了 ......
  • 【Spring Boot】Bean字段校验-JSR303
    规范:JSR303 BeanValidation1.0 开发过程:1、Bean定义字段校验规则:2、Controller引入@Valid(来自)或@Validated(来自org.springframework.validation.annotation)触发校验样例如下:背景知识:1.HibernateValidator定义Bean字段校验的注解和校验器实现......
  • RK3399驱动开发 | 01 -RK3399 gpio的使用(用户态和设备树两种方式)
    一、RK3399的GPIO  RK3399有5组GPIO,GPIO0-GPIO4,每一组GPIO成为一个GPIObank,每组GPIO包含32个引脚,需要注意,不是所有bank都有全部编号,例如GPIO4就只有C0-C7,D0-D2,所以一共有122个可用GPIO二、计算GPIO编号Friendly的开发板上板载一个状态LED,如下: 该LED接在GPIO0_B5......
  • JSON Web Tokens(JWT)
    JSONWebTokens(JWT)是一种用于身份验证和授权的开放标准。它可以在客户端和服务器之间安全地传输信息,并且非常适合以下场景:身份验证:JWT可以用于验证用户的身份。当用户登录后,服务器可以生成一个JWT并将其返回给客户端。客户端可以在后续的请求中将JWT作为身份验证凭证发送给服务器......
  • nodejs轻量服务器后端
    nodejs轻量服务器后端搭建思路server.js主函数+mine.js配置文件+index.html测试网页server.js文件varPORT=8080;//端口varDIR='test1';//用于存放html的目录varhttp=require('http');varurl=require('url');varfs=require('fs');var......
  • js在正则中使用变量
     将[]换行替换成需要上传的字段 ......
  • iwebsec-文件上传 01 前端JS过滤绕过
    01、题目分析事实上,文件上传过滤是很正常的,需要自行判断是前端过滤还是后端过滤,一般上传木马的时候可以进行抓包,如果直接点击上传直接弹出禁止上传的界面,而没有抓到数据包,那就说明是前端js过滤,但是这个题目已经说明了是前端js过滤,就不用再测试了02、文件上传先用哥斯拉创建一个......
  • C++入门到放弃(11)——继承
    ​继承是面向对象编程语言当中,最重要的部分,也是代码重用的一种重要形式。不知道为啥不能添加代码了,全部只能用图片替代了。1.基本形式首先继承的有三种基本形式,分别是public、private、protected,代表公有继承、私有继承和保护继承,之前在介绍作用范围的时候提过这三者的区别,但这......
  • rhel 6.5以编译方式安装mysql 5.5.18
    文档课题:rhel6.5以编译方式安装mysql5.5.18数据库:mysql5.5.18系统:rhel6.564位安装包:mysql-5.5.18.tar.gz1、卸载MariaDB--卸载系统自带的mysql和mariadb-lib.[root@MySQL5518-Master~]#rpm-qa|grepmysqlmysql-libs-5.1.71-1.el6.x86_64[root@MySQL5518-Master~......