javascript对象继承详解

问题

好比咱们有一个“动物”对象的构造函数。app

function animal() {
        this.type = '动物';
    }

还有一个“猫”对象的构造函数。函数

function cat(name,color) {
        this.name = name;
        this.color = color;
    }

咱们知道猫也属于动物,若是这个猫对象想要继承动物对象的属性,咱们该怎么作呢?this

构造函数绑定

使用构造函数绑定是最简单的方法,使用call或者apply将父对象绑定在自对象上就能够了。prototype

function cat(name,color) {
        animal.apply(this,arguments);
        this.name = name;
        this.color = color;
    }
    var cat1 = new cat("haha", 'red');
    console.log(cat1.type);  //动物

不过这种方法比较少见。code

拷贝继承

若是把父对象的全部属性和方法,拷贝进子对象,也能够实现继承。对象

function extend(Child, Parent) {
    var p = Parent.prototype;
    var c = Child.prototype;
    for (var i in p) {
      c[i] = p[i];
      }
    c.uber = p;     //桥梁做用
  }

使用方法:继承

extend(cat, animal);
    var cat1 = new cat("haha","red");
    alert(cat1.type);     // 动物

原型继承(prototype)

相比于上面的直接绑定,原型继承的方法比较常见,对于prototype,我本身简单总结了一下。内存

每一个函数都有一个prototype属性,这个属性是指向一个对象的引用,当使用new关键字建立新实例的时候,这个实例对象会从原型对象上继承属性和方法。原型

也就是说,若是将“猫”构造函数的prototype属性指向一个“动物”实例,那么再建立“猫”对象实例的时候,就继承了“动物”对象的属性和方法了。io

继承实例

cat.prototype = new animal();
    cat.prototype.constructor = cat;
    var cat1 = new cat("haha","red");
    console.log(cat1.constructor == cat);   //true
    console.log(cat1.type);  // 动物
  • 代码第一行,咱们将cat函数的prototype对象指向一个animal对象的实例(其中就包含了animal的type属性了)。

  • 代码第二行是什么意思呢?

    • 首先,假如咱们没有加这行代码,运行

      cat.prototype = new animal();
          console.log(cat.prototype.constructor == animal);  //true

      也就是说,其实每一个prototype对象都有一个constructor属性,指向它的构造函数。

    • 咱们再看下面的代码

      cat.prototype = new animal();
          var cat1 = new cat("haha", 'red');
          console.log(cat1.constructor == animal);   //true

      由上咱们看到实例cat1的构造函数是animal,因此,显然是不对的。。。cat1明明是new cat()才生成的,因此咱们应该手动纠正。cat.prototype对象的constructor值改成cat。

    • 因此这也是咱们应该注意的一点,若是咱们替换了prototype对象,就应该手动纠正prototype对象的constructor属性。

      o.prototype = {};
          o.prototype.constructor = o;

直接继承prototype

因为在animal对象中,不变的属性能够直接写在animal.prototype中。而后直接让cat.prototype指向animal.prototype也就实现了继承。

如今咱们先将animal对象改写成:

function animal() {
    }
    animal.prototype.type = '动物';

而后再实现继承:

cat.prototype = animal.prototype;
    cat.prototype.constructor = cat;
    var cat1 = new cat("haha","red");
    console.log(cat1.type); // 动物

与上一种方法相比,这种方法显得效率更高(没有建立animal实例),节省了空间。可是这样作正确吗?答案是不正确,咱们继续看。

cat.prototype = animal.prototype;

这行代码让cat.prototype和animal.prototype指向了同一个对象,因此若是改变了cat.prototype的某一个属性,都会反映到animal.prototype上,这显然不是咱们想要看到的。

好比咱们运行:

console.log(animal.prototype.constructor == animal)  //false

结果看到是false,为何呢?cat.prototype.constructor = cat;这一行就会把animal.prototype的constructor属性也改掉了。

利用空对象做为中介

var F = function(){};
    F.prototype = animal.prototype;
    cat.prototype = new F();
    cat.prototype.constructor = cat;

结合上面两种方法,由于F是空对象,因此几乎不占内存。这时修改cat的prototype对象,就不会影响到animal的prototype对象。

console.log(animal.prototype.constructor == animal);   // true

而后咱们将上面的方法封装一下:

function extend(Child, Parent) {
    var F = function(){};
    F.prototype = Parent.prototype;
    Child.prototype = new F();
    Child.prototype.constructor = Child;
    Child.uber = Parent.prototype;
  }

使用的时候,方法以下:

extend(cat,animal);
    var cat1 = new cat("haha","red");
    console.log(cat1.type); // 动物

Child.uber = Parent.prototype; 这行代码就是个桥梁做用,让子对象的uber属性直接指向父对象的prototype属性,等于在自对象上打开一条叫uber的通道,让子对象的实例可以使用父对象的全部属性和方法。

相关文章
相关标签/搜索