当前位置:网站首页>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
边栏推荐
- [thread knowledge points] - spin lock
- [C题目]牛客 链表中倒数第k个结点
- iframe嵌套其它网站页面 全屏设置
- 39 simple version of millet sidebar exercise
- Realsense ROS installation configuration introduction and problem solving
- Qt connect 中, SIGNAL,SLOT 与 lambda 对比
- Copy files / folders through Robocopy
- VS2010 add WAP mobile form template
- 44 新浪导航 ,小米边栏 练习
- 在win10系统下使用命令查看WiFi连接密码
猜你喜欢
随机推荐
Pl/sql creates and executes ORALCE stored procedures and returns the result set
密码强度验证示例
EDA chip design solution based on AMD epyc server
Detailed explanation of lio-sam operation process and code
[C topic] Li Kou 88. merge two ordered arrays
Vs2010添加wap移动窗体模板
Handle Oracle deadlock
[C topic] the penultimate node in the Niuke linked list
As methods for viewing and excluding dependencies
About RDBMS and non RDBMS [database system]
Nacos2.1.0 cluster construction
39 简洁版小米侧边栏练习
Scala111-map、flatten、flatMap
Bridge NF call ip6tables is an unknown key exception handling
在win10系统下使用命令查看WiFi连接密码
万能通用智能JS表单验证
Unable to start web server when Nacos starts
L1 and L2 regularization
Sublimetext-win10 cursor following problem
了解一下new的过程发生了什么








