组合继承

2018-11-18  本文已影响0人  SuYongZhi
//原型实现继承
//借用构造函数实现继承
//组合继承:原型继承+借用构造函数继承
        
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);
上一篇 下一篇

猜你喜欢

热点阅读