如何让子类继承父类的属性
1//第一种方法是最省事,但会让子类的constructor变成父类的constructor
function people() {
this.name = 'kay';
}
people.prototype.getName = function(){ return this.name }
function man() {
this.sex = 'male';
people.call(this);
}man.prototype = people.prototype;
var MAN = new man();
console.log(MAN.getName());
// 'kay'
【如何让子类继承父类的属性】2//第二种方法是最正确的
function people() {
this.name = 'snowin';
}
people.prototype.getName = function() { return this.name };
function man() {
this.sex = 'male';
people.call(this);
}
man.prototype = new people();
man.prototype.constructor = man;
var MAN = new man();
console.log(MAN.getName());
//'snowin'
推荐阅读
- 考研英语阅读终极解决方案——阅读理解如何巧拿高分
- 如何寻找情感问答App的分析切入点
- 诗歌:|诗歌: 《让我们举起世界杯,干了!》
- mybatisplus如何在xml的连表查询中使用queryWrapper
- MybatisPlus使用queryWrapper如何实现复杂查询
- 微习惯复盘
- 如何在Mac中的文件选择框中打开系统隐藏文件夹
- 漫画初学者如何学习漫画背景的透视画法(这篇教程请收藏好了!)
- java中如何实现重建二叉树
- Linux下面如何查看tomcat已经使用多少线程