当前位置:网站首页>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
边栏推荐
猜你喜欢

Docker上运行redis以配置文件方式启动,连接客户端报错Error: Server closed the connection

AS查看依赖关系和排除依赖关系的办法
![[Nacos] what does nacosclient do during service registration](/img/76/3c2e8f9ba19e36d9581f34fda65923.png)
[Nacos] what does nacosclient do during service registration

006 operator introduction

Introduction to raspberry Pie: initial settings of raspberry pie

密码强度验证示例

39 简洁版小米侧边栏练习

瀑布流布局

32 use of chrome debugging tools

如何解决Visual Stuido2019 30天体验期过后的登陆问题
随机推荐
Sublimetext-win10 cursor following problem
SQL Server forcibly disconnects
解决asp.net上传文件时文件太大导致的错误
Add the jar package under lib directory to the project in idea
如何解决Visual Studio中scanf编译报错的问题
安装EntityFramework方法
oracle_12505错误解决方法
防抖(debounce)和节流(throttle)
Scala111-map、flatten、flatMap
在win10系统下使用命令查看WiFi连接密码
32 use of chrome debugging tools
006 operator introduction
我的创作纪念日
《三子棋》C语言数组应用 --n皇后问题雏形
When using jetty to run items, an error is reported: form too large or form too many keys
44 Sina navigation, Xiaomi sidebar exercise
Implementation of redis distributed lock
AS查看依赖关系和排除依赖关系的办法
如何解决Visual Stuido2019 30天体验期过后的登陆问题
[C topic] Li Kou 88. merge two ordered arrays