JavaScript面向对象编程——构造函数继承

原文链接

本文转载自阮一峰老师的Javascirpt教学博客:Javascript面向对象编程(二):构造函数的继承


前言

上一篇文章介绍了JS里如何“封装”数据和方法,以及如何从原型对象生成实例。
这篇文章要介绍的,对象之间“继承”的五种方法。

例如,现在有一个Animal对象的构造函数:

function Animal() {
    this.species = "animal";
}

还有一个Cat对象的构造函数:

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

怎样才能使Cat继承自Animal呢?


构造函数绑定

第一种方法也是最简单的方法,使用call或者apply方法,将父对象的构造函数绑定在子对象上,即在子对象的构造函数中加一行:

function Cat(name, color) {
    Animal.apply(this, arguments);
    this.name = name;
    this.color = color;
}

var cat1 = new Cat("damao", "yellow");
console.log(cat1.species);  // animal

prototype模式

这种方法更常见,使用prototype属性。
如果Cat的prototype对象,指向一个Animal的实例,那么所有Cat的实例,就能继承Animal了。

Cat.prototype = new Animal();
Cat.prototype.constructor = Cat;
var cat1 = new Cat("damao", "yellow");
console.log(cat1.species);  // animal

代码的第一行,我们将Cat的prototype对象指向一个Animal的实例

Cat.prototype = new Animal();

它相当于完全删除了prototype对象原先的值,然后赋予一个新值。但是,第二行又是什么意思呢?

Cat.prototype.constructor = Cat;

原来,任何一个prototype对象都有一个constructor属性,指向它的构造函数。如果没有Cat.prototype = new Animal();这一行,Cat.prototype.constructor是指向Cat的,加了这一行以后,Cat.prototype.constructor指向了Animal。
可以测试一下:

function Animal() {
    this.species = "Animal";
}

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

Cat.prototype = new Animal();
console.log(Cat.prototype.constructor == Animal); // true
Cat.prototype.constructor = Cat;
console.log(Cat.prototype.constructor == Animal); // false
var cat1 = new Cat("damao", "yellow");
console.log(cat1.species);  // animal

每一个实力也有一个constructor属性,默认调用prototype对象的constructor属性。

console.log(cat1.constructor == Cat.prototype.constructor); // true

因此,需要手动修改Cat.prototype.constructor,使Cat的构造函数重新指向Cat。

这一点非常重要,编程时务必要遵守。下文都遵循了这一点,即如果替换了prototype对象:

o.prototype = {}

那么下一步,必然是为新的prototype对象加上constructor属性,并将这个属性指回原来的构造函数。

o.prototype.constructor = o;

直接继承prototype

这种方法是对第二种方法的改进。由于Animal对象中,不变的属性都可以直接写入Android.prototype。所以,我们也可以让Cat()跳过Animal(),直接继承Animal.prototype。

现在,我们先将Animal对象改写:

function Animal(){}
Animal.prototype.species = "Animal";

然后,将Cat的prototype对象,指向Animal的prototype对象,这样就完成了继承。

Cat.prototype = Animal.prototype;
Cat.prototype.constructor = Cat;
var cat1 = new Cat("damao", "yellow");
console.log(cat1.species); // Animal

与前一种方法相比,这样做的优点是效率比较高(不用执行和建立Animal实例了),比较省内存。缺点是Cat.prototype和Animal.prototype现在都指向了同一个对象,那么任何对Cat.prototype的修改,都会反映到Animal.prototype。

所以,上面这一段代码其实是有问题的。请看第二行:

Cat.prototype.constructor = Cat;

这一句实际上把Animal.prototype对象的constructor属性也改掉了。


利用空对象作为中介

由于”直接继承prototype“存在上述的缺点,所以就有第四种方法,利用一个空对象作为中介。

var F = function(){};
F.prototype = Animal.prototype;
Cat.prototype = new F();
Cat.prototype.constructor = Cat;

F是空对象,几乎不占用内存。这时,修改Cat的prototype对象,就不会影响到Animal的prototype对象。

我们可以将上面的方法,封装成一个函数,便于使用。

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("damao", "yellow");
console.log(cat1.species); // Animal

这个extend函数,就是YUI库如何实现继承的方法。
另外,说明一点,函数体最后一行:

Child.uber = Parent.prototype;

意思是为子对象设一个uber属性,这个属性直接指向父对象的prototype属性。这等于在子对象上打开一条通道,可以直接调用父对象的方法。这一行放在这里,只是为了实现继承的完备性,纯属备用性质。


拷贝继承

上面是采用prototype对象,实现继承。我们也可以换一种思路,纯粹采用”拷贝“方法实现继承。简单的说,如果把父对象的所有属性和方法,拷贝进子对象,不也能实现继承吗?这样我们就有了第五种方法。

首先,还是把Animal的所有不变属性,都放到它的prototype对象上。

function Animal(){}
Animal.prototype.species = "Animal";

然后,再写一个函数,实现属性拷贝的目的。

function extend2(Child, Parent) {
    var p = Parent.prototype;
    var c = Child.prototype;
    for (var i in p) {
        c[i] = p[i];
    }
    c.uber = p;
}

这个函数的作用,就是将父对象的prototype对象中的属性,一一拷贝给Child对象的prototype对象

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值