当前位置:网站首页>JS inheritance
JS inheritance
2022-04-23 03:16:00 【GeXueliu】
Inherit
js Inheritance in is implemented through prototypes .
Mode one : Point the prototype to the prototype of the inherited object
function Animal(name) {
this.name =name;
}
Animal.prototype.age = 23;
let a = new Animal('lily');
function People(name) {
this.name = name
}
People.prototype = Animal.prototype; // Specify the prototype object as Animal The prototype of the
let p1 = new People(' Zhang San ');
console.log(p1.age) // expect output 23 p1 Inherited age
/*** * This raises a problem , When the modification is passed p1 modify Person The prototype of , It was also modified Animal The prototype of the **/
p1.__proto__.age = 48;
console.log(a.age) // expect output 48 Animal The prototype has been modified

Mode two : Specify the prototype object as an instance of the inherited object
function Animal(name) {
this.name =name;
}
Animal.prototype.age = 23;
let a = new Animal('lily');
function People(name) {
this.name = name
}
People.prototype = new Animal('dog'); // Inherit Animal
let p2 = new People('wangermazi');
p2.__proto__.age = 15;
console.log(a.age) // expect output 23
console.log(p2.age); // expect output 15
/* * Solved the problem of mode one , At the same time, method 3 can also solve the problems existing in method 1 ***/

Mode three : Use transfer function
function Animal(name) {
this.name =name;
}
Animal.prototype.age = 23;
let a = new Animal('lily');
function People(name) {
this.name = name
}
function fn() {
}; // Transfer function
fn.prototype = Animal.prototype;
People.prototype = new fn(); // Inherit
let p3 = new People('lisi');
p3.__proto__.age = 48
console.log(p3.age) // expect output 48
console.log(a.age) // expect output 23

summary : So inherit a function , It's best not to use mode 1 ( Direct the prototype to the prototype of the function to be inherited ). Realization bind Function will use this knowledge point
版权声明
本文为[GeXueliu]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220627322952.html
边栏推荐
- IDEA查看历史记录【文件历史和项目历史】
- It can receive multiple data type parameters - variable parameters
- General testing technology [1] classification of testing
- 可以接收多种数据类型参数——可变参数
- 交换二叉树中每个结点的左和右
- Fiddler use
- This new feature of C 11, I would like to call it the strongest!
- The most easy to understand dependency injection and control inversion
- Preview of converting doc and PDF to SWF file
- Queue storage and circular queue
猜你喜欢

Student achievement management

【VS Code】解决jupyter文件在vs code中显示异常的问题

超好用的Excel异步导出功能

2022山东省安全员C证上岗证题库及在线模拟考试
![Use of ADB command [1]](/img/e6/fb8fd6956c226f75b831f8eb46277f.png)
Use of ADB command [1]

Impact of AOT and single file release on program performance

. net tip: talk about the problem that the scoped service cannot be obtained in the middleware structure

Peut recevoir plusieurs paramètres de type de données - paramètres variables

svg标签中利用<polygon/>循环数组绘制多边形

C语言实现通讯录----(静态版本)
随机推荐
《C语言程序设计》(谭浩强第五版) 第7章 用函数实现模块化程序设计 习题解析与答案
一文了解全面静态代码分析
Find the number of leaf nodes of binary tree
[Mysql] LEFT函數 | RIGHT函數
Impact of AOT and single file release on program performance
Two methods are used to solve the "maximum palindrome product" problem
Top 9 task management system in 2022
准备一个月去参加ACM,是一种什么体验?
be based on. NETCORE development blog project starblog - (1) why do you need to write your own blog?
月薪10k-20k都无法回答的事务问题,你会吗?
Five tips for cross-border e-commerce in 2022
Top ten project management software similar to JIRA
What kind of experience is it to prepare for a month to participate in ACM?
Student achievement management
JSON related
数据挖掘系列(3)_Excel的数据挖掘插件_估计分析
搭建XAMPP时mysql端口被占用
Tencent video price rise: earn more than 7.4 billion a year! Pay attention to me to receive Tencent VIP members, and the weekly card is as low as 7 yuan
MySql分组查询规则
2022 Shandong Province safety officer C certificate work certificate question bank and online simulation examination