当前位置:网站首页>The implementation process of inheritance and the difference between Es5 and ES6 implementation
The implementation process of inheritance and the difference between Es5 and ES6 implementation
2022-07-25 15:09:00 【Henry_ Nan】
ES5 See this article for the inheritance method of :ES5 The idea of writing inheritance
ES5 And ES6 Inheritance method comparison of :
ES5
function Parent() {
this.name = 'parent';
this.arr = [1,2,3,4];
}
Parent.prototype.say = function () {
console.log('say');
};
function Child(age) {
Parent.call(this);
this.age = age;
}
Child.prototype = Object.create(Parent.prototype);
Child.prototype.constructor = Child;
var c = new Child(12);
console.log(c.name); // Output parent
c.say(); // Output say
console.log(c.constructor); // Output function Child(age) {Parent.call(this);this.age = age;}
console.log(new Parent().constructor); // Output Parent() {this.name = 'parent';this.arr = [1,2,3,4];}
ES6
class Parent2 {
constructor() {
this.name = 'parent';
}
}
Parent2.prototype.say = function () {
console.log('say');
};
class Child2 extends Parent {
constructor(age) {
super();
this.age = age;
}
}
var c2 = new Child2(12);
console.log(c2.name); // Output parent
c2.say(); // Output say
console.log(c.constructor); // Output function Child(age) {Parent.call(this);this.age = age;}
console.log(new Parent().constructor); // Output Parent() {this.name = 'parent';this.arr = [1,2,3,4];}
- ES5 The essence of inheritance is to create instance objects of subclasses first , Then add the method of the parent class to this On (Parent.apply(this)), Then inherit the prototype chain .
- ES6 The inheritance mechanism is quite different , In essence, you create an instance object of the parent class first this( So you have to call the parent class's super() Method , Just can use this keyword , Otherwise, the report will be wrong .), And then modify it with the constructor of the subclass this Implementation inheritance .
ps:es5 And the following cannot be inherited perfectly array, Relevant contents can be searched by yourself
边栏推荐
- "How to use" decorator mode
- MySQL之事务与MVCC
- Fast-lio: fast and robust laser inertial odometer based on tightly coupled IEKF
- 【JS高级】js之正则相关函数以及正则对象_02
- 39 simple version of millet sidebar exercise
- pl/sql 创建并执行oralce存储过程,并返回结果集
- 防抖(debounce)和节流(throttle)
- Solve the error caused by too large file when uploading file by asp.net
- C, c/s upgrade update
- "How to use" observer mode
猜你喜欢

Sudo rosdep init error ROS installation problem solution

AS查看依赖关系和排除依赖关系的办法

Syntax summary of easygui

Boosting之GBDT源码分析

什么是物联网

Gonzalez Digital Image Processing Chapter 1 Introduction

Nacos2.1.0 cluster construction

006 operator introduction

Client error: invalid param endpoint is blank

npm的nexus私服 E401 E500错误处理记录
随机推荐
sql server强行断开连接
About RDBMS and non RDBMS [database system]
Overview of cloud security technology development
树莓派入门:树莓派的初始设置
45padding won't open the box
[thread knowledge points] - spin lock
API health status self inspection
As methods for viewing and excluding dependencies
MFC 线程AfxBeginThread基本用法,传多个参数
JS 同步、异步,宏任务、微任务概述
Scala110-combineByKey
流程控制(上)
Log4j2 basic configuration
防抖(debounce)和节流(throttle)
[C topic] Li Kou 88. merge two ordered arrays
String type time comparison method with error string.compareto
Nacos2.1.0 cluster construction
LeetCode_字符串_中等_151.颠倒字符串中的单词
基于OpenCV和YOLOv3的目标检测实例应用
bridge-nf-call-ip6tables is an unknown key异常处理