当前位置:网站首页>Prototype inheritance
Prototype inheritance
2022-07-24 12:44:00 【Liang wha duck,】
Give Way P2 Inherit P1, The following will be P1 Attribute to P2, This method is not recommended
<script>
function P1() {
this.name = arguments[0];
this.age = arguments[1];
}
P1.prototype.sayName = function () {
alert(this.name);
};
// By copying , To inherit P1
function P2() {
this.name = arguments[0];
this.age = arguments[1];
}
</script>
function P2() {
// obj2 = this
this.name = arguments[0];
this.age = arguments[1];
}
// In the instance P2 when , Yes .
// Then these attributes are also attached to the object of the instance
new P2();
Then we just need to P1 You can mount the properties in to the object of the instance
function P1(opt) {
this.name = opt.name;
this.age = opt.age;
}
P1.prototype.sayName = function () {
alert(this.name);
};
function P2(opt) {
// Inherit all of the base class
P1.call(this, opt)
// Expands itself
this.sex = opt.sex;
}
// Scheme 1 : Because the prototype is an object , Reference type data . If you will P1 The archetype of is given to P2, Then when expanding P2 The prototype is , It will also modify P1
/*P2.prototype = P1.prototype;*/
// Option two : send P1 The object of the instance obj1 Mount to P2 On the prototype of
P2.prototype = new P1({});
// At this time P2 On the prototype , It won't affect P1 The prototype of the
P2.prototype.add = function () {
alert(this.name)
}
const o1 = new P1({
name: 'Lee',
age: 18
});
const o2 = new P2({
name: ' Liang what duck ',
age: 18,
sex: 'girl'
});
o1.sayName();
o2.add();
When the output o2 
This is because when P2.prototype = new P1({});, hold P1 The private property of extends to P2 The prototype of
But logically, it does not affect , Because the private attribute to be inherited
To remove these values is undefined Properties of , Available scheme 3 , With the help of a useless function
// Option three :
function c() {} // c Class has no private properties , It's empty.
c.prototype = P1.prototype
P2.prototype = new c();
Output o2
There is another scheme that has nothing to do with the previous three schemes 4
The main reason for the elimination of scheme 1 is that they point to the same memory address , To modify a , Another prototype is also modified
// Option four : It has nothing to do with the previous three schemes
for(var key in P1.prototype) {
P2.prototype[key] = P1.prototype[key];
}
Scheme 4 cannot traverse P1 The prototype of the prototype
Scheme 3 is recommended , Can traverse to
边栏推荐
- 做自媒体视频剪辑有免费可商用的素材网站吗?
- leetcode第 302 场周赛复盘
- TypeNameExtractor could not be found
- Cluster construction based on kubernetes v1.24.0 (II)
- leecode-268. 丢失的数字(异或的应用,找没有出现的数字,找只出现一次的数字)
- 猿人学第七题
- Buckle practice - 31 effective tic tac toe games
- C language course design -- hotel management system
- leetcode-81. 搜索旋转排序数组 II(二分查找返回true/false)
- Wechat applet generates QR code
猜你喜欢

How to find out the function calling process of complex code running as soon as possible

Industry insight | how to better build a data center? It and business should "go together"

手把手教你用 Power BI 实现 4 种可视化图表

基于Kubernetes v1.24.0的集群搭建(一)

【功能测试】项目的测试——登录和发布文章功能

如何将Typora中图片上传到csdn

QT based software framework design

ASP. Net core deployment Manual: 1. Deployment Basics

What can breaking through the memory wall bring? See the actual battle of volcano engine intelligent recommendation service to save money and increase efficiency

基于Kubernetes v1.24.0的集群搭建(三)
随机推荐
Take chef and ansible as examples to get started with server configuration
How to render millions of 2D objects smoothly with webgpu?
基于Kubernetes v1.24.0的集群搭建(二)
我在一个模块工程中使用注解配置了redis的序列化, 然后在另外一个模块引入这个模块,为什么这个配置
Use typeface to set the text font of textview
Wechat applet - drawing dashboard
2022.07.15 暑假集训 个人排位赛(十)
泰山OFFICE技术讲座:段落边框的布局难点
Is it safe to contact the account manager online to open a fund account?
如何用WebGPU流畅渲染百万级2D物体?
Unity rotation test
Conference publishing function of conference OA project
Node takes effect after using NVM to install under Windows system, but NPM does not take effect
leetcode第 302 场周赛复盘
手把手教你用 Power BI 实现 4 种可视化图表
Summary of recent interviews
1.4.1 many, many symbols (cin/cout unbinding, O3 optimization)
Wechat applet generates QR code
Okaleido tiger NFT is about to log in to binance NFT platform
国产旗舰手机定价近六千,却连iPhone12都打不过,用户选谁很明确