如今有一个”动物”对象的组织函数。
function Animal() {
this.species = "动物";
}
另有一个”猫”对象的组织函数。
function Cat(name, color) {
this.name = name;
this.color = color;
}
怎样才能使”猫”继续”动物”呢?
一、 组织函数绑定
第一种要领也是最简朴的要领,运用call
或apply
要领,将父对象的组织函数绑定在子对象上,即在子对象组织函数中加一行:
function Cat(name, color) {
Animal.apply(this, arguments); // 到场这行代码
this.name = name;
this.color = color;
}
var cat1 = new Cat("大毛", "黄色");
console.log(cat1.species); // "动物"
二、 prototype形式
第二种要领更罕见,运用prototype
属性。
假如”猫”的prototype
对象,指向一个Animal
的实例,那末一切”猫”的实例,就可以继续Animal
了。
Cat.prototype = new Animal();
Cat.prototype.constructor = Cat;
var cat1 = new Cat("大毛", "黄色");
console.log(cat1.species); // "动物"
代码的第一行,我们将Cat
的prototype
对象指向一个Animal的实例。它相当于完整删除了prototype
对象本来的值,然后给予一个新值。
任何一个prototype
对象都有一个constructor
属性,指向它的组织函数。
假如没有Cat.prototype = new Animal();
,Cat.prototype.constructor
是指向Cat
的;加了这行代码以后,Cat.prototype.constructor
就会指向Animal
。
更主要的是,每个实例也有一个constructor
属性,默许挪用prototype
对象的constructor
属性。
因而,在运转Cat.prototype = new Animal()
这一行以后,cat1.constructor
也指向Animal
。
这显然会致使继续链的杂乱(cat1
明显是用组织函数Cat
天生的),因而我们必需手动改正,将Cat.prototype
对象的constructor
值改成Cat
。这就是第二行代码的作用。
这是很主要的一点,请务必注重:
假如替换了prototype对象,下一步必定是为新的prototype对象加上constructor属性,并将这个属性指回本来的组织函数。
o.prototype = {};
o.prototype.constructor = o;
三、 直接继续prototype
第三种要领是对第二种要领的革新。
因为Animal
对象中,稳定的属性都可以直接写入Animal.prototype
。所以,我们也可以让Cat()
跳过 Animal()
,直接继续Animal.prototype
。
如今,我们先将Animal对象改写:
function Animal() {}
Animal.prototype.species = "动物"
然后,将Cat
的prototype
对象指向Animal
的prototype
对象,如许就完成了继续。
Cat.prototype = Animal.prototype;
Cat.prototype.constructor = Cat;
var cat1 = new Cat("大毛", "黄色")
console.log(cat1.species) // "动物"
与前一种要领比拟,如许做的长处是效力比较高(不必实行和竖立Animal的实例了),比较省内存。
瑕玷是Cat.prototype
和Animal.prototype
如今指向了同一个对象,那末任何对Cat.prototype
的修正,都邑反应到Animal.prototype
。
所以上述代码实际上是有题目标,第二行Cat.prototype.constructor = Cat
实际上把Animal.prototype.constructor
也改成了Cat
。
四、 应用空对象作为中介
因为”直接继续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("大毛", "黄色")
console.log(cat1.species)
这个extend函数,就是YUI库怎样完成继续的要领。
别的,申明一点,函数体末了一行Child.uber = Parent.prototype
,意义是为子对象设一个uber属性,这个属性直接指向父对象的prototype属性。(uber是一个德语词,意义是”向上”、”上一层”。)这等于在子对象上翻开一条通道,可以直接挪用父对象的要领。这一行放在这里,只是为了完成继续的完整性,纯属备用性子。
五、 拷贝继续
上面是采纳prototype对象,完成继续。我们也可以换一种思绪,地道采纳”拷贝”要领完成继续。简朴说,假如把父对象的一切属性和要领,拷贝进子对象,不也可以完成继续吗?如许我们就有了第五种要领。
起首,照样把Animal的一切稳定属性,都放到它的prototype对象上。
function Animal() {}
Animal.prototype.species = "动物"
然后,再写一个函数,完成属性拷贝的目标。
function extend(Child, Parent) {
var p = Parent.prototype;
var c = Child.prototype;
for (var i in p) {
c[i] = p[i]
}
c.uber = p;
}
这个函数的作用,就是将Parent对象的prototype对象中的属性,拷贝给Child对象的prototype对象。
运用要领
extend(Cat, Animal);
var cat1 = new Cat("大毛", "黄色")
console.log(cat1.species)