当前位置:网站首页>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
边栏推荐
- A set of combination boxing to create an idea eye protection scheme
- Fundamentals of software testing and development
- What kind of experience is it to prepare for a month to participate in ACM?
- Yes Redis using distributed cache in NE6 webapi
- If the deep replication of objects is realized through C #?
- 2022g2 boiler stoker examination question bank and online simulation examination
- 再战leetcode (290.单词规律)
- 2022 Shandong Province safety officer C certificate work certificate question bank and online simulation examination
- 2022年做跨境电商五大技巧小分享
- Flink实时数仓项目—DWS层设计与实现
猜你喜欢

Use of slice grammar sugar in C #

be based on. NETCORE development blog project starblog - (1) why do you need to write your own blog?

IOTOS物联中台对接海康安防平台(iSecure Center)门禁系统

Is it difficult to choose binary version control tools? After reading this article, you will find the answer

超好用的Excel异步导出功能

Chapter 9 of C language programming (fifth edition of Tan Haoqiang) analysis and answer of exercises for users to establish their own data types

Comprehensive calculation of employee information

手机连接电脑后,QT的QDIR怎么读取手机文件路径

PID debugging of coding motor (speed loop | position loop | follow)

Yes Redis using distributed cache in NE6 webapi
随机推荐
Configure automatic implementation of curd projects
Configuration table and page information automatically generate curd operation page
中后二叉建树
Miniapi of. Net7 (special section): NET7 Preview3
2022G2电站锅炉司炉考试题库及在线模拟考试
Load view Caton
Using positive and negative traversal to solve the problem of "the shortest distance of characters"
MySQL port is occupied when building xampp
The most detailed in the whole network, software testing measurement, how to optimize software testing cost and improve efficiency --- hot
2022g2 boiler stoker examination question bank and online simulation examination
Five tips for cross-border e-commerce in 2022
ASP. Net and ASP NETCORE multi environment configuration comparison
Mysql database
Mysql database, inconsistent index character set, slow SQL query, interface timeout
The most easy to understand dependency injection and control inversion
Source generator actual combat
Establishing and traversing binary tree
General test technology [II] test method
PID debugging of coding motor (speed loop | position loop | follow)
为什么BI对企业这么重要?