JavaScript 完美继承

我们知道,父类中的属性有this、static和prototype三种。我们常用的继承方式有两种:

  1. 原型继承:可以把父类的prototype属性全部继承下来
  2. 对象冒充:可以将父类的this属性继承下来,再配合for...in循环,可以将父类的static属性也继承下来

将这两种方式综合使用,并在此基础上,使用delete删除不需要的属性,以及通过重写方法来覆盖父属性,以达到完美继承的效果。


/**
 * Filename: PrefectExtend.js
 * Description: 该程序将通过原型继承、对象冒充和覆盖三套机制,来完美实现
 * 	Js的继承。
 * Date: 2012/3/7
 */

/**
 * 父类Animal,及其this、静态、prototype属性的构造。
 */
Animal = function(tail) {
	this.tail = tail || "动物的尾巴";	// 父类的this属性
	this.kinds = "动物";
	Animal.instanceCounter ++;			// 父类的静态属性
}
Animal.instanceCounter = 0;
Animal.prototype = {					// 父类的prototype属性
	happy: function() {
		console.log("摇动>" + this.tail);
	},
	eat: function() {
		console.log("动物吃生的");
	},
	run: function() {
		console.log("动物四条腿跑");
	},
	fight: function() {
		console.log("动物往死里打");
	}
}
// 这里如果不这样做,那么Animal.prototype.constructor == Object,详见参考资料[2]
Animal.prototype.constructor = Animal;

/**
 * 1. 对象冒充: 继承this及static属性
 */
// 子类Person
Person = function(name) {
	Person.superclass.call(this);	// this属性继承
	delete this.tail; // 删除不需要的this属性
	this.name = name || "猎空de代码";
	for(var p in Animal) {			// static属性继承
		Person[p] = Animal[p];
	}
}
Person.superclass = Animal;
/**
 * 2. 原型继承
 */
F = function() {} // 空壳函数,用于清除Animal的构造属性(this和静态)
F.prototype = Animal.prototype;
Person.prototype = new F();
delete Person.prototype.fight;	//删除不需要的prototype属性
Person.prototype.constructor = Person;
/**
 * 3. prototype方法覆盖
 */
Person.prototype.eat = function() {
	console.log(this.name + "吃熟的");
}

// 创建实例
var person = new Person();
console.log("姓名: " + person.name);
console.log(person.kinds);
console.log("实例数目" + Person.instanceCounter);
person.happy();
person.eat();
person.run();


程序运行结果:

参考资料:

[1] Ext 江湖,大漠穷秋,北京:电子工业出版社,2012.1 (这段程序,基本是模仿该书写的)

[2] JavaScript类和继承:constructor属性


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值