当前位置:网站首页>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.浅拷贝和深拷贝_深拷贝
- MySQL(4)
- [C language brush leetcode] 735. Planetary collision (m)
- LeetCode_ Dynamic programming_ Medium_ 264. Ugly number II
- What does the Red Cross in the SQL editor mean (toad and waterdrop have been encountered...)
- Data warehouse: Practice of hierarchical structure of data warehouse in banking industry
- 1. Mx6ul core module use serial RTC test (XII)
- 2. Login - verification code function and saving login status
- Brief introduction and use of NPM link
猜你喜欢

Prometheus + redis exporter + grafana monitor redis service

力扣148:排序链表

Be careful about bitmap, the "memory Assassin"~
![[C]详解语言文件操作](/img/12/4affa1d3fb3e4ee126e1c1e3872d9b.png)
[C]详解语言文件操作

Ggplot2 learning summary

MySQL(4)

微信小程序解密并拆包获取源码教程
![[2020] [paper notes] growth of bi2te3/cofeb double-layer heterojunction by magnetron sputtering——](/img/5d/7d26e2d0d832c95e1cc011995ce774.png)
[2020] [paper notes] growth of bi2te3/cofeb double-layer heterojunction by magnetron sputtering——

由一个数据增量处理问题看到技术人员的意识差距

Yum install MySQL FAQ
随机推荐
[2021] [paper notes] biological effects of cell membrane under infrared and THz - effect is a phenomenon, action is a mechanism - the benefits of THz to medicine
Monitoring of debezium synchronization debezium
2. Login - verification code function and saving login status
我来图书馆小程序签到流程分析
Handling process of the problem that the virtual machine's intranet communication Ping fails
Prove that perfect numbers are even
Effectively solve the problem of garbled code when idea runs the web project (with detailed steps)
SQL how to return all data when the input query condition is empty
租户问题。
ERROR: could not extract tar starting at offset 000000000000020980+9231072+2
Tenant issues.
[C language brush leetcode] 1462. curriculum IV (m)
图解B+树的插入过程
19_ Request forms and documents
Slow query log in MySQL
Error reporting caused by local warehouse
Prometheus + process exporter + grafana monitor the resource usage of the process
[early knowledge of activities] list of recent activities of livevideostack
1. Mx6ul core module use serial RTC test (XII)
Brief introduction and use of NPM link