js中的继承

要使一个子类继承父类,需要两步实现:
第一:继承父类自身的属性和方法
第二:继承父类原型上的方法

  • "父类自身"属性和方法的继承

    function Person(name, age){
      this.name = name;
      this.age = age;
      this.sayHello = function() {
        console.log('hello');
      };
    }
    
    Person.prototype.sayName = function(){
      console.log(this.name);
    }
    
    Person.prototype.sayAge = function(){
      console.log(this.age);
    }
    
    function Student(name, age, sex) {
      // 如果要继承其他类的属性和方法,就是需要改变this的指向——将this指向使用Student创建的实例对象;为什么是改变this的指向呢?考虑使用new创建一个对象实例需要执行的四个步骤,就可以理解为什么需要改变this了
      
      //方法一:使用call改变this,传入参数列表(call与apply不同的是call传入的是参数列表,而apply传入的是数组或类数组对象)
      Peron.call(this, name, age);
    
      //方法二:使用apply改变this
      Person.apply(this, arguments);
    
      //方法三:使用bind改变this,返回一个新函数,并且将this指向传入的第一个参数(注意:是返回一个新函数,bind后面的this指向的是使用Student创建的实例对象)
      Person.bind(this)(name, age);
    
      this.sex = sex;
    }
    
    var hexon = new Student('hexon', 17, 'male');
    console.log(hexon.name);    
    console.log(hexon.age);
    console.log(hexon.sex);
    hexon.sayHello();
    // 以下两句报错,提示hexon上没有这两个方法
    hexon.sayName();
    hexon.sayAge();
    
    

    注意:以上只能继承"父类自身"的属性和方法,并不能继承Person原型上的方法

  • 继承"父类"原型上的方法
    要继承父类原型上的方法,就是要将子类的prototype指向父类的prototype

    两种方法:

    第一种: Object.create()方法
    Object.create()方法:创建一个拥有指定原型和若干属性的对象,并将其返回

    function Person(name, age){
      this.name = name;
      this.age = age;
      this.sayHello = function() {
        console.log('hello');
      };
    }
    Person.prototype.sayName = function(){
      console.log(this.name);
    }
    Person.prototype.sayAge = function(){
      console.log(this.age);
    }
    function Student(name, age, sex) {
      // 如果要继承其他类的属性和方法,就是需要改变this的指向——将this指向使用Student创建的实例对象;为什么是改变this的指向呢?考虑使用new创建一个对象实例需要执行的四个步骤,就可以理解为什么需要改变this了
      
      //方法一:使用call改变this,传入参数列表(call与apply不同的是call传入的是参数列表,而apply传入的是数组或类数组对象)
      Peron.call(this, name, age);
    
      //方法二:使用apply改变this
      Person.apply(this, arguments);
    
      //方法三:使用bind改变this,返回一个新函数,并且将this指向传入的第一个参数(注意:是返回一个新函数,bind后面的this指向的是使用Student创建的实例对象)
      Person.bind(this)(name, age);
    
      this.sex = sex;
    }
    
    // 将Student的原型指向Prson的原型对象
    Student.prototype = Object.create(Person.prototype);
    // 因为prototype原型对象中有一个constructor,它指向对象的构造函数,
    // Peroson.prototype.constructor ==> Person,因此Student.prototype.constructor ==> Person,
    // 所以需要手动将Student.prototype.constructor ==> Student
    Student.prototype.constructor = Student;
    //以上两句便完成了原型的继承
    Student.prototype.saySex = function(){
      console.log(this.sex);
    }
    var hexon = new Student('hexon', 17, 'male');
    console.log(hexon.name);    
    console.log(hexon.age);
    console.log(hexon.sex);
    hexon.sayHello();
    // 以下两句就不会报错
    hexon.sayName();    // 输出 hexon
    hexon.sayAge();     // 输出 17
    

    第二种方法:

    function fn(){}         // 创建一个空函数
    fn.prototype = Person.prototype;  // 将该函数的prototype指向Person.prototype
    Student.prototype = new fn();   // new 一个fn()对象实例,该对象实例的原型对象指向Person.prototype,这样Student.prototype就指向了Person.prototype
    Student.prototype.constructor = Student;
    
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 工厂模式类似于现实生活中的工厂可以产生大量相似的商品,去做同样的事情,实现同样的效果;这时候需要使用工厂模式。简单...
    舟渔行舟阅读 7,827评论 2 17
  • 一、面向过程和面向对象的区别、联系 1.面向过程编程:注重解决问题的步骤,分析问题需要的每一步,实现函数依次调用。...
    空谷悠阅读 906评论 1 11
  • 继承有什么作用? (难度:3*) 继承可以使一个对象直接使用另一个对象的属性和方法。 有几种常见创建对象的方式? ...
    coolheadedY阅读 536评论 0 0
  • 单例模式 适用场景:可能会在场景中使用到对象,但只有一个实例,加载时并不主动创建,需要时才创建 最常见的单例模式,...
    Obeing阅读 2,097评论 1 10
  • var a = 1; console.log(typeof a);// 'number' var b = '1';...
    zdnexus阅读 336评论 0 0