美文网首页
组合继承

组合继承

作者: SuYongZhi | 来源:发表于2018-11-18 16:13 被阅读0次
    //原型实现继承
    //借用构造函数实现继承
    //组合继承:原型继承+借用构造函数继承
            
    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);
    

    相关文章

      网友评论

          本文标题:组合继承

          本文链接:https://www.haomeiwen.com/subject/rregfqtx.html