组合继承

//原型实现继承
//借用构造函数实现继承
//组合继承:原型继承+借用构造函数继承
        
function Person(name,age,sex) {
    this.name = name;
    this.age = age;
    this.sex = sex;
}
Person.prototype.sayHi = function() {
    console.log("你好");
};
function Student(name,age,sex,score) {
    //借用构造函数:属性值重复的问题
    Person.call(this,name,age,sex);
    this.score = score;
}
//改变原型的指向---继承
Student.prototype = new Person();//不传值
Student.prototype.eat = function () {
    console.log("吃东西");
}
//实例化对象
var stu = new Student("小明",20,"男","100分");
console.log(stu.name,stu.age,stu.sex,stu.score);

var stu1 = new Student("小黑",25,"女","90分");
console.log(stu1.name,stu1.age,stu1.sex,stu1.score);
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容