当前位置:网站首页>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
边栏推荐
- 35 quick format code
- Splice a field of the list set into a single string
- ESXI6.7.0 升级到7.0U3f(2022年7月12 更新)
- 用setTimeout模拟setInterval定时器
- Sudo rosdep init error ROS installation problem solution
- Handle Oracle deadlock
- Deployment and simple use of PostgreSQL learning
- System. Accessviolationexception: an attempt was made to read or write to protected memory. This usually indicates that other memory is corrupted
- API health status self inspection
- vscode 插件篇收集
猜你喜欢

API health status self inspection

Raft of distributed consistency protocol

oracle_ 12505 error resolution

41 picture background synthesis - colorful navigation map

Add the jar package under lib directory to the project in idea

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

Nacos2.1.0 cluster construction

39 简洁版小米侧边栏练习

一个程序最多可以使用多少内存?

43 box model
随机推荐
Scala110-combineByKey
C语言函数复习(传值传址【二分查找】,递归【阶乘,汉诺塔等】)
sql server强行断开连接
继承的实现过程及ES5和ES6实现的区别
什么是物联网
TypeScript学习2——接口
27 选择器的分类
How to realize a correct double check locking
pkg_resources动态加载插件
43 盒子模型
Leetcode combination sum + pruning
SublimeText-win10光标跟随问题
Scala110-combineByKey
深入:微任务与宏任务
Splice a field of the list set into a single string
Universal smart JS form verification
树莓派入门:树莓派的初始设置
[C题目]力扣88. 合并两个有序数组
27 classification of selectors
瀑布流布局