当前位置:网站首页>JS inheritance and prototype chain
JS inheritance and prototype chain
2022-07-03 01:04:00 【Duck:)】
One 、 class class
stay es6 in , have access to class Keyword declares a class , Then instantiate the object with this class .
Class abstracts the common part of an object , It refers to a general category Object refers to a particular , By instantiating a concrete object
Inherit : Subclasses can inherit some properties and methods of the parent class
class Father{
constructor(x,y){
this.x = x;
this.y = y;
}
sum(){
console.log(this.x + this.y);
console.log(this.x)
}
say(){
return ' I'm daddy '
}
}
class Son extends Father{
constructor(x,y){
super(x,y);// Called the constructor in the parent class , Must be in subclass this Previous call
this.x = x;
this.y = y;
}
say(){
console.log(super.say() + ' Son ')//super Call the normal function of the parent class
}
}
var son = new Son(1,2)
son.sum();
son.say();
1. Class constructor Constructors
There is a constructor function , You can receive the passed parameters , And return the instance object
constructor Function as long as new When generating an instance , This function will be called automatically , If you don't write constructor This function , Class will also automatically generate this function .
The common attributes of the class are placed in constructor Inside
There is no need to write all the functions in the class function; No more , Division
2.extends Let the child class inherit the parent class
super keyword : You can call the constructor( Constructors ), You can also call the normal function of the parent class .
Subclass in constructor (constructor) Use in super, Must be on this front ( You must first call the constructor of the parent class , Then use the subclass construction method )
3. In the inheritance , If you instantiate a subclass and output a method , Let's see if the subclass has this method , If there is, execute the subclass first
In the inheritance , If the subclass doesn't have , To find whether the parent class has this method , If there is , Just execute this method of the parent class .( Nearby principle )
4. stay es6 The middle class has no variable promotion , So you have to define the class first , To instantiate an object through a class
The common properties and methods in the class must be added with this
Two 、 Constructors
A constructor is a special function , It's mainly used to initialize objects ( That is, assign values to object member variables ), It's always related to new Use it together . We can extract some public properties and methods from objects , Then encapsulate it into this function .
Instance members :( The function passes through this Added members ) Can only be accessed through instantiated methods
Static members :( Members added to the constructor itself ) Static members can only be accessed through constructors
new What did you do during execution ?
(1) Create a new empty object in memory(2) Give Way this Point to this new object
(3) Execute the code in the constructor , Add properties and methods to this new object
(4) Return to this new object ( So you don't need to return)
function Star(uname,uage){
this.uname = uname;// Instance members
this.uage = uage;
this.sing = function(){
console.log(' I sing ')
}
}
var ldh = new Star(' Lau Andy ',20);
console.log(ldh.uname);
ldh.sing()//1. Instance members ( The function passes through this Added members ) Can only be accessed through instantiated methods
//Star.sing()// Report errors
Star.sex = ' male ';//2. Static members ( Members added to the constructor itself )
console.log(ldh.sex)//undefined
console.log(Star.sex)// male Static members can only be accessed through constructors
Constructor method is easy to use , But there is a problem of wasting memory
In the constructor The same way In every time new When it comes to an example , Will open up a space in the pile . We want this constant method to take up only one space , So there's a constructor prototype .
3、 ... and 、 Constructor prototype prototype
JS Specify that every constructor has a prototype attribute , Point to another object . We can take those unchanging methods , Directly defined in prototype On the object , In this way, all instances of objects can share these methods .
Four 、es5 Inherit and es6 The difference between inheritance
Inherit : Subclasses can inherit some properties and methods of the parent class
(1)Es5 The essence of inheritance is to create instance objects of subclasses first , Then add the method of the parent class to this On (Father.call(this)).
Es6 The essence of inheritance is to create the instance object of the parent class first this, And then modify it with the constructor of the subclass this.
(2)Es5 The inheritance of is implemented through constructors and prototypes
Es6 adopt class Keyword definition class , Class has construction methods , Between classes through keywords extends Implementation inheritance . Subclasses must be constructor Call in method super Method , Otherwise, an error will be reported for the new instance . Because subclasses don't have their own this object , It inherits from the parent class this object , And then process it . If you don't call super Method , Subclasses will not get this object .
5、 ... and 、es5 Define a class , And implement inheritance
Combination inheritance
function Father(name,age){
this.name = name;
this.age = age;
}
Father.prototype.work = function(){
// Father's method
console.log(' Father works ')
}
function Son(name,age){
Father.call(this,name,age)// Inherit the properties of the parent class ( stay father Zhongba this An instance that points to a child constructor )
}
Son.prototype.study = function(){
console.log(' Children go to school ')
}
Son.prototype = new Father();// Methods that inherit the parent class ( Direct assignment , Modified the of the son prototype object constructor)
Son.prototype.constructor = Son;// Put your son's constructor Revise it back
var son = new Son(' Xiao Ming ',18);
console.log(son);
console.log(son.__proto__.constructor)
console.log(son instanceof Father);
Parasitic combinatorial inheritance
function Father(name,age){
this.name = name;
this.age = age
}
Father.prototype.work = function(){
console.log(' Father works ')
}
function Son(name,age){
Father.call(this,name,age);// Inherit the properties of the parent class
}
Son.prototype.study = function(){
console.log(' Children go to school ')
}
Son.prototype = Object.create(Father.prototype);
// Methods that inherit the parent class : First create a temporary constructor , Then take the father's prototype object as the prototype of this constructor , Finally, return a new instance of the temporary type
// Then let the son's prototype object point to a new instance of the temporary type ( In this way, inheritance can be realized )
Son.prototype.constructor = Son;
var son = new Son('aaa',23);
console.log(Son.prototype);
console.log(son.__proto__.constructor)
object.create It's equivalent to the following create function ( Do not write )
function create(obj) {
function F() {
}// First create a temporary constructor
F.prototype = obj;// The incoming object is then used as the prototype for the constructor
return new F();// Returns a new instance of the temporary type
}
边栏推荐
- Arduino开发之按键检测与正弦信号输出
- [daily training] 871 Minimum refueling times
- First hand evaluation of Reza electronics rz/g2l development board
- Leetcode-224: basic calculator
- 瑞萨RZ/G2L ARM开发板存储读写速度与网络实测
- (C语言)数据的存储
- [introduction to AUTOSAR seven tool chain]
- 指针进阶(一)
- 删除有序链表中重复的元素-II
- Leetcode-1964: find the longest effective obstacle race route to each position
猜你喜欢
Initial order of pointer (basic)
研发一款国产ARM智能边缘计算网关需要什么
数学建模之线性规划(含MATLAB代码)
[case sharing] let the development of education in the new era advance with "number"
【AutoSAR 一 概述】
Arduino开发之按键检测与正弦信号输出
Linear programming of mathematical modeling (including Matlab code)
excel IF公式判断两列是否相同
[AUTOSAR five methodology]
Key detection and sinusoidal signal output developed by Arduino
随机推荐
Leetcode-2280: represents the minimum number of line segments of a line graph
18_微信小程序之微信视频号滚动自动播放视频效果实现2.0
【爱死机】《吉巴罗》被忽略的细节
电话网络问题
First hand evaluation of Reza electronics rz/g2l development board
[case sharing] let the development of education in the new era advance with "number"
FPGA - 7系列 FPGA内部结构之Clocking -04- 多区域时钟
Machine learning: numpy version linear regression predicts Boston house prices
Basic use of sringcloud & use of component Nacos
matlab 多普勒效应产生振动信号和处理
瑞萨RZ/G2L ARM开发板存储读写速度与网络实测
Test shift right: Elk practice of online quality monitoring
How to convert Quanzhi a40i/t3 to can through SPI
寻找标杆战友 | 百万级实时数据平台,终身免费使用
【AutoSAR 七 工具链简介】
[introduction to AUTOSAR seven tool chain]
lex && yacc && bison && flex 配置的問題
正确甄别API、REST API、RESTful API和Web Service之间的异同
删除有序链表中重复的元素-II
【AutoSAR 十一 通信相关机制】