当前位置:网站首页>ES6高级-利用原型对象继承方法
ES6高级-利用原型对象继承方法
2022-07-26 02:24:00 【setTimeout()】
我们来看下面的代码:
<body>
<script>
function Father(uname, age) {
this.uname = uname;
this.age = age;
}
Father.prototype.money = function() {
console.log('money!');
}
function Son(uname, age) {
Father.call(this, uname, age);
}
Son.prototype = Father.prototype;
Son.prototype.exam = function() {
console.log('exam');
}
var son = new Son('son', 18);
console.log(son);
console.log(Father.prototype);
</script>
</body>输出结果:

我们就可以发现如果直接把父类的原型对象赋给子类的原型对象,虽然子类可以继承父类的方法,但是如果子类添加方法的话就会对父类造成影响。
原因是因为看下面这个图:

把子原型对象指向了父原型对象,所以修改子原型对象也会修改父原型对象,所以得换一个方法实现方法继承。
更改代码为:
<body>
<script>
function Father(uname, age) {
this.uname = uname;
this.age = age;
}
Father.prototype.money = function() {
console.log('money!');
}
function Son(uname, age) {
Father.call(this, uname, age);
}
Son.prototype = new Father();
Son.prototype.exam = function() {
console.log('exam');
}
var son = new Son('son', 18);
console.log(son);
console.log(Father.prototype);
</script>
</body>输出结果为:

这样就正常了,原理看下面这张图:

但是上面的代码也有问题,就是这句:Son.prototype = new Father();,这样写的话Son的prototype就没有constructor了,所以需要指回原来的原型对象,加上Son.prototype.constructor=Son();
边栏推荐
- 1. Mx6ul core module serial USB interface test (VI)
- I.MX6UL核心模块使用连载-USB接口测试 (六)
- [red team] att & CK - using bits services to achieve persistence
- Is the securities account presented by qiniu true? How to open it safely and reliably?
- 微信小程序解密并拆包获取源码教程
- scipy.sparse.csr_matrix
- Adruino basic experimental learning (I)
- 商业智能BI全解析,探寻BI本质与发展趋势
- I.MX6UL核心模块使用连载-WIFI测试 (八)
- The third question of leetcode 302 weekly Games -- query the number with the smallest k after cutting the number
猜你喜欢

Bo Yun container cloud and Devops platform won the trusted cloud "technology best practice Award"
![[2021] [paper notes] 6G technology vision - otfs modulation technology](/img/50/577ad05bc16e80d1c68eec7b6da988.png)
[2021] [paper notes] 6G technology vision - otfs modulation technology

I came to the library applet check-in process analysis

1. Mx6ul core module uses serial NAND FLASH read / write test (III)

Primary key b+ tree, secondary index b+ tree and corresponding query process analysis

Keil's operation before programming with C language
![[xxl-job] xxl-job learning](/img/2c/d3872983e4228a3ef52a9d1bef836e.png)
[xxl-job] xxl-job learning

I.MX6UL核心模块使用连载-nand flash读写测试 (三)

Kaggle registration method to solve the problem of man-machine verification

【云原生】4.1 DevOps基础与实战
随机推荐
栈题目:文件的最长绝对路径
MySQL建Websites数据表
关于mysql的问题,希望个位能帮一下忙
Audio and video technology development weekly | 254
18.删除链表的倒数第n个节点
[red team] att & CK - using bits services to achieve persistence
2022-07-17
[C language brush leetcode] 443. Compressed string (m)
Master-slave replication in MySQL
墨天轮高分技术文档分享——数据库安全篇(共48个)
我来图书馆小程序签到流程分析
19_ Request forms and documents
TCP三次握手四次挥手
I.MX6UL核心模块使用连载-以太网测试 (七)
Manifold learning
Be careful about bitmap, the "memory Assassin"~
[xxl-job] xxl-job learning
ggplot2学习总结
What can EAM system help enterprises do?
Adruino 基础实验学习(一)