当前位置:网站首页>JS parasitic combinatorial inheritance
JS parasitic combinatorial inheritance
2022-07-27 11:51:00 【Big carp Yu】
Prototype inheritance
child.prototype=new Parent()
function A(x){
this.x=x
}
A.prototype.getX=function(){
console.log(this.x)}
function B(y){
this.y=y
}
B.prototype.gety=function(){
console.log(this.y)}
B.prototype=new A()
B.prototype.constructor=B// because B The prototype object of points to A An example of , Then its constructor will point to A, Therefore, we need to change the direction of the constructor in the prototype object
Let the prototype of the subclass point to the instance of the parent class , The prototype chain of the instance of the parent class will point to the prototype of the class . This method has great shortcomings : When the subclass instance is modified, the parent instance will also change , Because here is the way of using references , That is, operate in the same memory space 
Object.create()
Create a new object , Using an existing object to provide a newly created object __proto__
That is to provide your object with a __proto__
Inheritance combined inheritance
Yes, it is call Inheritance and Object.create
function Person(name){
this.name=name
}
function A(sex){
Person.call(this,'A extends Person')
this.sex=sex
}
Person.prototype.getName=function(){
console.log(this.name)
}
A.prototype=Object.create(Person.prototype)
A.prototype.constructor=A
call The purpose of inheritance is to Person The private attribute of is inherited as its own private attribute , Each of the following A Instances of will have a private property name=’A extends Person‘, But only call Inheritance is not enough , Here we call the parent constructor as a normal method , But we can't get the properties and methods on the parent prototype object . Therefore, we need to point the child class prototype to the parent class prototype . So give A Of prototype Prototype objects provide Person.prototype, such A Instance __proto__ Point to A Of prototype,A Of prototype Of __proto__ Point to Person Of prototype. This combinatorial inheritance gets the private properties and methods of the parent class and becomes its own private properties and methods , We also got the public properties and methods of the parent class . Is in es6 The best way before inheritance
In the picture A Namely Person Parent class 
边栏推荐
- 剑指 Offer 笔记: T58 - I. 翻转单词顺序
- Greek alphabet reading
- Firewall firewall
- EfficientNet
- Leetcode 04: T26. Delete duplicate items in the sorting array (simple); Sword finger offer 67. convert the string to an integer (medium); Interview question 01.08. zero matrix (simple)
- Leetcode 03: t58. Length of the last word (simple); Sword finger offer 05. replace spaces (simple); Sword finger offer 58 - ii Rotate string left (simple)
- LAN SDN technology hard core insider 11 the key of cloud convergence CP -- hierarchical port binding
- Adobe Audition提示 音频输入的采样率与输出设备不匹配——问题解决
- 【无标题】多模态模型 CLIP
- LAN SDN technology hard core insider 13 from LAN to Internet
猜你喜欢

PWM的原理和PWM波的产生

Shell编程之正则表达式(Shell脚本文本三剑客之grep)

IDEA: Can‘t use Subversion command line client:svn 解决方案

82.(cesium之家)cesium点在3d模型上运动

【无标题】多模态模型 CLIP

Shell脚本文本三剑客之awk

Weibo comment crawler + visualization

箱型图介绍

SMA TE: Semi-Supervised Spatio-Temporal RepresentationLearning on Multivariate Time Series

Principle of control system based on feedback rate
随机推荐
LAN SDN hard core technology insider 23 looking forward to the future - RDMA (Part 1)
JS-寄生组合式继承
1. Introduction and basic use of flume
SMA TE: Semi-Supervised Spatio-Temporal RepresentationLearning on Multivariate Time Series
Finding the finite zero point of transfer function under different sampling periods
暂用 Solo,博客选择困难
The first case of monkeypox in pregnant women in the United States: the newborn was injected with immunoglobulin and was safely born
SMA TE: Semi-Supervised Spatio-Temporal RepresentationLearning on Multivariate Time Series
LNMP架构搭建(部署Discuz论坛)
开源flink有写holo的Datastream connector或者flink sql conn
go入门篇 (2)
Weibo comment crawler + visualization
LAN SDN technology hard core insider 11 the key of cloud convergence CP -- hierarchical port binding
Sword finger offer notes: T53 - ii Missing numbers from 0 to n-1
shell编程之免交互
1.Flume 简介及基本使用
Why is ack=seq+1 when TCP shakes hands three times
多种进制之间的转换
JUC框架 从Runnable到Callable到FutureTask 使用浅析
局域网SDN硬核技术内幕 23 展望未来——RDMA(上)