Persom Teacher
call能够实现属性的继承

  1. // Persom Teacher
  2. // call能够实现属性的继承
  3. function Person(name,age){
  4. this.name = name;
  5. this.age = age;
  6. }
  7. function Teacher(name,age,skill){
  8. var self = this;
  9. Person.call(self,name,age)
  10. this.skill = skill;
  11. }
  12. var t = new Teacher("zhang",18,"js");
  13. console.log(t);

demo

        function Person(name,age){
            this.name = name;
            this.age = age;
        }
        Person.prototype.sayName = function(){
            console.log(this.name);
        }
        function Teacher(name,age,skill){
            Person.call(this,name,age);
            this.skill = skill;
        }

        // 方法的继承
        Teacher.prototype = Person.prototype;
        Teacher.prototype.sayAge = function(){
            console.log(this.age);
        }
        Teacher.prototype.constructor = Teacher;
        var t = new Teacher("zhang",18,"js");
        var p = new Person("lisi",13);
        console.log(t.constructor);

es5中实现继承的方法

方法的继承

        function Person(name,age){
            this.name = name;
            this.age = age;
        }
        Person.prototype.sayName = function(){
            console.log(this.name);
        }
        function Teacher(name,age,skill){
            Person.call(this,name,age);
            this.skill = skill;
        }
        // 方法的继承
        Teacher.prototype = Object.create(Person.prototype,{
            constructo:{
                value:Teacher
            }
        })
        Teacher.prototype.sayAge = function(){
            console.log(this.age);
        }
        var t = new Teacher("zhang",18,"js");
        console.log(t.constructor);

拓展

        // function Person(name,age){
        //     this.name = name;
        //     this.age = age;
        // }
        // Person.prototype = {
        //     constructor:Person,
        //     sayName(){
        //         console.log(this.name);
        //     }
        // }
        class Person{
            constructor(name,age){
                this.name = name;
                thisage = age;
            }
            sayName(){
                console.log(this.name)
            }
        }
        var p = new Person("heng",19)
        console.log(p);