es5实现继承 --- 组合继承

在“前端之巅”公众号上看到的,地址: 前端之巅

    function Person(name) {
        this.name = name;
        this.gender = ["男", "女"];
    }

    Person.prototype.sayHi = function() {
        console.log("Hi, This is ", this.name);
    }

    function Teacher(name, age) {
        Person.call(this, name);
        this.age = age;
    }

    Teacher.prototype = new Person();
    Teacher.prototype.constructor = Person;

    Teacher.prototype.sayAge = function() {
        console.log("This teacher's age is ", this.age);
    }

    var teacherone = new Teacher('Jack', 28);
    teacherone.sayAge();
    teacherone.sayHi();
    console.log(teacherone.__proto__);

 

你可能感兴趣的:(学习笔记)