当前位置:网站首页>Es5 thinking of writing inheritance
Es5 thinking of writing inheritance
2022-07-25 15:09:00 【Henry_ Nan】
stay js in , There are several methods of inheritance :
Constructor inheritance
function Parent() {
this.name = 'parent';
}
function Child(age) {
Parent.call(this);
this.age = age;
}
var c = new Child(12);
console.log(c.name); // Output parent
The principle is Child Use in constructor call Changed the this Point to , bring Child The object has been increased name attribute , The value is ’parent’, Completed inheritance .
The disadvantage of this method is : Things on the prototype chain of parent functions cannot be inherited
function Parent() {
this.name = 'parent';
}
Parent.prototype.say = function () {
console.log('say');
};
function Child(age) {
Parent.call(this);
this.age = age;
}
var p = new Parent(); //new One Parent Object for comparison
p.say(); // Output say
var c = new Child(12);
c.say(); //undifined
say yes Parent The method on the prototype chain ,Parent When an object calls a method , If you don't exist, go back to the prototype chain to find , Found on the prototype chain say Method , and Child Object does not inherit Parent Prototype chain of objects , So when it looks up, it can't find , Output undifined.
Prototype chain inheritance
function Parent() {
this.name = 'parent';
}
Parent.prototype.say = function () {
console.log('say');
};
function Child(age) {
this.age = age;
}
Child.prototype = new Parent();
var c = new Child(12);
console.log(c.name); // Output parent
c.say() // Output say
Prototype chain inheritance is to directly let Child Constructor's prototype Direct to Parent object , such Parent Things that are Child Object can be found directly from its prototype chain .
The disadvantage of this method is : When creating multiple instances , If different instances may affect each other , for example :
function Parent() {
this.name = 'parent';
this.arr = [1,2,3,4]
}
Parent.prototype.say = function () {
console.log('say');
};
function Child(age) {
this.age = age;
}
Child.prototype = new Parent();
var c1 = new Child(12);
var c2 = new Child(12);
console.log(c1.arr); //[1,2,3,4]
console.log(c2.arr); //[1,2,3,4]
c1.arr.push(5);
console.log(c1.arr); //[1,2,3,4,5]
console.log(c2.arr); //[1,2,3,4,5]
Here only for c1 Of arr It's changed , But it affected c2 Of arr, The reason is this way of inheritance , The prototype of the instance is the same object , namely new Parent() Coming out Parent example , When instance search arr If you can't find it here, you will find it on the prototype chain Parent Properties of , eureka arr, and arr It is also a reference type , therefore c1 modify arr when ,c2 Of arr It will change .
Constructor and prototype chain inheritance combination method
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 = new Parent();
var c1 = new Child(12);
var c2 = new Child(12);
console.log(c1.arr); //[1,2,3,4]
console.log(c2.arr);//[1,2,3,4]
c1.arr.push(5);
console.log(c1.arr); //[1,2,3,4,5]
console.log(c2.arr); //[1,2,3,4]
The difference between this combination and the previous method is , When the instance is created Parent In the properties of the , So I won't go Parent seek arr, Modified arr It is in their respective attributes arr, There will be no impact , At the same time, he inherited Parent Things on the prototype chain .
Here's an optimization :
Before Child.prototype = new Parent() That is, the second inheritance method above , To inherit Parent The prototype chain inherits at the same time Parent What's in the function , Now in Child Function has been used Parent.call(this) Directly inherited Parent What's in the function , Then the prototype chain that directly inherits the parent function is finished, that is :
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 = Parent.prototype;
var c = new Child(12);
console.log(c.name); // Output parent
c.say(); // Output say
After optimization , There are still shortcomings :
console.log(c.constructor); // Output Parent() {this.name = 'parent';this.arr = [1,2,3,4];}
Find... Here Child The prototype of constructor What we found was Parent Of constructor, The reason is because Child.prototype = Parent.prototype It's going to be Parent Of prototype Directly assign to Child Of prototype, So it's constructor Must be Parent Of constructor, At this time, it cannot be modified directly :
Child.prototype = Parent.prototype;
Child.prototype.constructor = Child;
console.log(c.constructor); // Output function Child(age) {Parent.call(this);this.age = age;}
console.log(new Parent().constructor); // Output function Child(age) {Parent.call(this);this.age = age;}
This is because Parent.prototype( object ) Is a reference type , So modify Child Of constructor,Parent Of constructor It will change too. , So will Child.prototype = Parent.prototype; Revise it :
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);
c.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];}
use Object.create(Parent.prototype) This method creates a new object , Assign a value to Child.prototype, Such changes Child Of constructor when ,Parent Of constructor Will not be affected .
This is the most perfect way
ps:es5 And the following cannot be inherited perfectly array, Relevant contents can be searched by yourself
边栏推荐
- MeanShift聚类-01原理分析
- 《三子棋》C语言数组应用 --n皇后问题雏形
- Share a department design method that avoids recursion
- [C topic] Li Kou 88. merge two ordered arrays
- SublimeText-win10光标跟随问题
- 图片裁剪cropper 示例
- [C topic] force buckle 876. Intermediate node of linked list
- Award winning interaction | 7.19 database upgrade plan practical Summit: industry leaders gather, why do they come?
- "Ask every day" briefly talk about JMM / talk about your understanding of JMM
- JS 同步、异步,宏任务、微任务概述
猜你喜欢

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

Boosting之GBDT源码分析

Gonzalez Digital Image Processing Chapter 1 Introduction

6月产品升级观察站

37 element mode (inline element, block element, inline block element)

Yarn: the file yarn.ps1 cannot be loaded because running scripts is prohibited on this system.

oracle_12505错误解决方法

45padding won't open the box

用setTimeout模拟setInterval定时器

41 picture background synthesis - colorful navigation map
随机推荐
Copy files / folders through Robocopy
6月产品升级观察站
sql server强行断开连接
用OpenPose进行单个或多个人体姿态估计
PHP 通过原生CURL实现非阻塞(并发)请求模式
LeetCode_因式分解_简单_263.丑数
Scala110-combineByKey
Handle Oracle deadlock
Melody + realsense d435i configuration and error resolution
我的创作纪念日
L1 and L2 regularization
pl/sql 创建并执行oralce存储过程,并返回结果集
Stored procedure bias of SQL to LINQ
35 quick format code
SQL Server forcibly disconnects
如何解决Visual Studio中scanf编译报错的问题
[comprehensive pen test] difficulty 4/5, classic application of line segment tree for character processing
Pl/sql creates and executes ORALCE stored procedures and returns the result set
06. Neural network like
iframe嵌套其它网站页面 全屏设置