当前位置:网站首页>Creation and inheritance of JS class
Creation and inheritance of JS class
2022-07-24 16:20:00 【XIAOLINZI ˖ °】
If you want to know js Class creation and inheritance , We need to master js Prototype and prototype chain .
1. A brief introduction
js Prototype :
JavaScript Regulations , Every function has a prototype Object properties , It points to another object ( On the prototype chain ).prototype All properties and methods on , Will be inherited by an instance of the constructor . We can put invariable public properties and methods , Directly defined in prototype Object properties .
Prototype chain :
Connection between instance object and prototype , It's called the prototype chain .
__ proto __( Implicit connection ) Attribute is an implicit attribute of every object and function . For each contains __proto__ attribute , What he points to is the one who created his constructor prototype. The prototype chain is built through this attribute .
function Pro() {
//...
}
var pro = new Pro();
among Pro There is prototype attribute , and pro No, , But in pro It's implicit in __proto__ Attribute points to Pro.prototype.
That is to say :
pro.__proto__ === Pro.prototype; //true
__proto__ Attribute is an implicit attribute of every object and function . For each contains __proto__ attribute , What he points to is the one who created his constructor prototype. The prototype chain is built through this attribute .
function A() {
}
function B() {
}
var a1 = new A();
B.prototype = a1;
var b1 = new B();
It can be found in a chain :
b1.__proto__ === a1; //true
b1.__proto__.__proto__ === A.prototype; //true
b1.__proto__.__proto__.__proto__ === Object.prototype; //true
2. Class creation (es5)
First new One function, In this function Of prototype Add properties and methods to .
Create a Animal class :
// Define an animal class
function Animal(name) {
this.name = name; // attribute
this.sleep = function() {
// Example method
console.log(this.name + 'sleep...');
}
}
// Add a prototype method to the prototype chain
Animal.prototype.eat = function(food) {
console.log(this.name + 'eat...' + food)
}
3. Class inheritance
- Prototype chain inheritance :
Prototype chain inheritance is JavaScript The main way to implement inheritance . Use prototypes to let one reference type inherit the properties and methods of another .
// Create an empty object
function Cat() {
}
Cat.prototype = new Animal();
Cat.prototype.name = 'cat';
var cat = new Cat;
It can be observed through the console :
Print cat Object finds that its instance inherits Animal Properties and methods of .
characteristic : Based on prototype chain , Is an instance of a parent class , Is also an instance of a subclass .
shortcoming : Multiple inheritance cannot be implemented .
- Construct inheritance :
Use the constructor of the parent class to enhance the child class instance , Equal to Copy The instance attribute of the parent class is given to the child class ( No prototypes ).
function Cat() {
Animal.call(this);
this.name = name || 'cat';
}
var cat = new Cat();
Console observation :
You can see that the attributes from the parent class are inherited (name) And methods (sleep()), But there is no method defined on the prototype chain eat(). Further verify :
characteristic : Multiple inheritance can be realized .
shortcoming : Only the properties and methods of the inherited parent class can be implemented , Cannot inherit properties and methods on the prototype chain .
- Combination inheritance :
It is equivalent to a combination of construction inheritance and prototype chain inheritance . Construct... By calling the parent class , Inherit the properties of the parent class and keep the advantages of passing parameters , Then by using the parent instance as the child prototype , Realize function reuse .
function Cat() {
Animal.call(this);
this.name = name || 'cat';
}
Cat.prototype = new Animal();
Cat.prototype.constructor = Cat;
var cat = new Cat();
Console output , By comparing the previous introduction Prototype chain inheritance The console output , It is obvious that it calls two parent class constructors .
shortcoming : Called the parent constructor twice , Two examples have been generated .
- Parasitic combination inheritance :
It can be said to be the optimization of composite inheritance . By parasitizing , Cut off the instance properties of the parent class , such , When calling the construction of the parent class twice , The instance will not be initialized twice .
function Cat() {
Animal.call(this);
this.name = name || 'cat';
}
(function() {
// Create an empty object
var Super = function() {
};
// Prototype instances as subclasses
Super.prototype = Animal.prototype;
Cat.prototype = new Super();
}) ()
var cat = new Cat();
Console output :
Continuous updating ...
边栏推荐
- 如何防止跨站点脚本 (XSS) 攻击完整指南
- [LeetCode]75.颜色分类——题解(执行用时击败90% ,内存消耗击败 78%)
- 机器学习笔记 - 构建推荐系统(5) 前馈神经网络用于协同过滤
- Error 1053: the service did not respond to the start or control request in a timely fashion
- vscode常用快捷键
- leetcode:162. 寻找峰值【二分寻找峰值】
- Dedecms editor supports automatic pasting of word pictures
- 若依 this.$router.push 同地址不同参,页面不刷新问题
- Leetcode 231. 2 的幂
- Chapter 2 using API Mgmnt service
猜你喜欢
![[SWT] scrolling container to realize commodity list style](/img/84/07e7c794aaef3fb64f173b50150b21.png)
[SWT] scrolling container to realize commodity list style

狗牙根植物介绍
![Leetcode:162. looking for peak [two points looking for peak]](/img/77/64b7c9bf1aebc2a0ab82218ddfff62.png)
Leetcode:162. looking for peak [two points looking for peak]

Machine learning notes - building a recommendation system (5) feedforward neural network for collaborative filtering

Telephone system rules

Mysql8 encountered the problem of stopping after the service was started

How to choose the appropriate data type for fields in MySQL?

C# TCP客户端窗体应用程序异步接收方式

如何防止跨站点脚本 (XSS) 攻击完整指南

MySQL之知识点(十二)
随机推荐
[LeetCode]38.报数——题解(执行用时击败91% ,内存消耗击败 97%)
普林斯顿微积分读本02第一章--函数的复合、奇偶函数、函数图像
Minor record
多线程(基础)
With this machine learning drawing artifact, papers and blogs can get twice the result with half the effort!
Mysql8 encountered the problem of stopping after the service was started
[leetcode] day103 search two-dimensional matrix II
如何防止跨站点脚本 (XSS) 攻击完整指南
REST风格
MySQL之知识点(十二)
Machine learning notes - building a recommendation system (5) feedforward neural network for collaborative filtering
OpenMP入门
Is it safe for Anxin securities to open an account on mobile phone?
TCP协议调试工具TcpEngine V1.3.0使用教程
Code shoe set - mt2095 · zigzag jump
.net review the old and know the new: [6] what is LINQ
torch_ How to use scatter. Scatter() in detail
聊聊C指针
在 PHP Web 应用程序中防止 XSS 的最佳实践
Chapter 2 using API Mgmnt service