日期:2014-05-16  浏览次数:20464 次

JavaScript学习之三 — JavaScript实现继承的7种方式
本文列举了《JavaScript高级程序设计:第二版》书中讲到的6种创建JavaScript对象的模式,这里有英文版下载。
代码里边用到的一些公用方法本文后边有附。附件为源码。
1、原型链
Gmis.inheritance.Animal = function(nickname, legCount, characters) {
	this.nickname = nickname;
	this.legCount = legCount;
	this.characters = characters;
}
Gmis.inheritance.Animal.prototype = {
	constructor : Gmis.inheritance.Animal,
	getNickName : function() {
		return this.nickname;
	},
	getLegCount : function() {
		return this.legCount;
	},
	getCharacters : function() {
		return this.characters;
	},
	getCharacterString : function() {
		return "[" + (this.characters == undefined ? "undefined" : this.characters.join(",")) +"]";
	},
	toString : function() {
		return "nickname: " + this.getNickName() + ", legCount: " + this.getLegCount() + ", charachters: "
				+ this.getCharacterString();
	}
};
/**
 * 1、原型链<br>
 * 优点:继承了超类的方法--在通过new创建实例的时候,已经给实例添加了一个指向原型的属性,对原型方法的调用都是通过该隐藏实例查找过去的<br>
 * 缺点:<br>
 * 1、没有继承超类的属性<br>
 * 2、实例的构造函数是超类的构造函数<br>
 * 
 * @param {String} type
 * @param {int} legCount
 */
Gmis.inheritance.Cat = function(nickname, legCount) {
	this.nickname = nickname;
	this.legCount = (legCount == undefined) ? 4 : legCount;
};
Gmis.inheritance.Cat.prototype = new Gmis.inheritance.Animal();
var cat = new Gmis.inheritance.Cat("jiafei", 4, ['lovely', 'agile']);
Gmis.inheritance.util.printAnimal("1、原型链", cat.toString());
Gmis.util.printMessage("Gmis.inheritance.Cat.prototype.constructor:\n" + Gmis.inheritance.Cat.prototype.constructor);
Gmis.util.printMessage("cat.constructor:\n" + cat.constructor);
Gmis.util.printMessage("Gmis.inheritance.Cat.prototype.constructor"
		+ (Gmis.inheritance.Cat.prototype.constructor === cat.constructor ? " === " : " !== ") + "cat.constructor: ");

1、原型链 nickname: jiafei, legCount: 4, charachters: [undefined]
Gmis.inheritance.Cat.prototype.constructor:
function (nickname, legCount, characters) {
	this.nickname = nickname;
	this.legCount = legCount;
	this.characters = characters;
}
cat.constructor:
function (nickname, legCount, characters) {
	this.nickname = nickname;
	this.legCount = legCount;
	this.characters = characters;
}
Gmis.inheritance.Cat.prototype.constructor === cat.constructor

2、借用构造函数
/**
 * 2、借用构造函数<br>
 * 优点:可以继承属性<br>
 * 缺点:不能继承方法<br>
 * 
 * @param {String}  nickname
 * @param {int}  legCount
 */
Gmis.inheritance.Dog = function(nickname, legCount) {
	Gmis.inheritance.Animal.call(this, nickname, legCount);
};
Gmis.inheritance.Dog.prototype.toString = function() {
	return "Dog.... nickname: " + this.nickname + ", legCount: " + this.legCount;
};
var dog = new Gmis.inheritance.Dog("huhu", 4);
Gmis.inheritance.util.printAnimal("2、借用构造函数", dog.toString());

2、借用构造函数 Dog.... nickname: huhu, legCount: 4

3、组合继承
/**
 * 3、组合继承--组合借用构造函数和原型链两种方式,各取其利<br>
 * 优点:<br>
 * 1、可以继承属性<br>
 * 2、可以继承方法<br>
 * 3、子类的构造函数指向子类本身<br>
 * 缺点:<br>
 * 1、调用了两次超类构造函数,如下所示,分别在a1、a2出调用了超类构造函数<br>
 * 2、覆盖超类的方法的时候,没有办法在复用超类方法的基础上添加新的功能,如下toString方法<br>
 * 
 * @param {String} nickname
 * @param {int} legCount
 * @param {String} desc
 */
Gmis.inheritance.Pig = function(nickname, legCount, characters, desc) {
	// a1、第二次调用构造函数
	Gmis.inheritance.Animal.call(this, nickname, legCount, characters);
	this.desc = desc;
};
// a2、第一次调用构造函数
Gmis.inheritance.Pig.prototype = new Gmis.inheritance.Animal();
Gmis.inheritance.Pig.prototype.constructor = Gmis.inheritance.Pig;
Gmis.inheritance.Pig.prototype.getDesc = function() {
	return this.desc;
};
Gmis.inheritance.Pig.prototype.toString = function()