1.

  1. <script>
  2. //原型对象:javascript 的继承是基于原型的继承
  3. function Person(name,age){
  4. this.name=name;
  5. this.age=age;
  6. }
  7. Person.prototype.sayName=function(){
  8. console.log(this.name);
  9. }
  10. function Teacher(name,age,skill){
  11. Person.call(this,name,age); //call能够实现属性的继承
  12. this.skill=skill;
  13. }
  14. Teacher.prototype.sayAge=function(){
  15. console.log(this.age);
  16. }
  17. Teacher.prototype=Person.prototype;
  18. Teacher.prototype.constructor=Teacher;
  19. var t=new Teacher("zhang",18,"js");
  20. var p=new Person("lisi",13)
  21. console.log(p.constructor);
  22. </script>

2.

<script>
        /*面向对象的三种方法
        1.封装
        2.继承
        3.多态(自带多态)
        */

        //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); //call能够实现属性的继承
            this.skill=skill;
        }
        Teacher.prototype.sayAge=function(){
            console.log(this.age);
        }
        Teacher.prototype=Object.create(Person.prototype,{
            constructor:{
                value:Teacher
            }
        })
        var t=new Teacher("zhang",18,"js");
        console.log(t);
        t.sayName();  //检查是否继承
</script>