JavaScript继承的方式

1.构造函数绑定方式:

		<script type="text/javascript">
			function Animal(name,skill){
				this.name = name;
				this.skill = skill;
			}
			function Cat(name,skill){
//				Animal.call(this,name,skill);//和apply方法作用相同,只是apply第二个参数为数组
				Animal.apply(this,[name,skill]);//this指针重定向
			}
			var cat = new Cat("猫","抓老鼠");
			alert(cat.name+"的技能是"+cat.skill);
		</script>
原理:使用call或apply函数改变this指针的指向,实现继承.

2.原型链方式:

		<script type="text/javascript">
			function Dog() {}
			Dog.prototype.type = "狗";
			Dog.prototype.skill = function() {
				alert("狗的技能是捕猎");
			}
			function GuideDogs() {}
			GuideDogs.prototype = new Dog();
			var guideDogs = new GuideDogs();
			alert(guideDogs.type); //狗
			guideDogs.skill(); //狗的技能是捕猎
		</script>
prototype对象是个模板,要实例化的对象都以这个模板为基础.总而言之,prototype对象的任何属性和函数都被传递给那个类的所有实例.原型链利用这种功能来实现继承机制.

原型链继承方式缺点:不支持多重继承,原型链会用另一类型的对象重写类的prototype属性.子类的所有属性和方法都必须出现在prototype属性被赋值后.因为在它之前赋值的所有方法都会被删除.因为prototype属性被替换成了新对象,添加了新方法的原始对象将被销毁.
3.混合方式:

		<script type="text/javascript">
			function NBA(team, home) {
				this.team = team;
				this.home = home;
			}
			NBA.prototype.say = function() {
				alert("美国男子职业篮球联赛");
			}
			function EastTeam(team, home) {
				NBA.call(this, team, home); //this指针重定向
			}
			EastTeam.prototype = new NBA();
			var celtics = new EastTeam("Celtics", "Boston"); //原型链继承方式
			alert(celtics.team); //输出"Celtics"
			celtics.say(); //输出"美国男子职业篮球联赛"
		</script>
JavaScript中实现继承的方式还有很多种,这三种是比较常用的方式.以上实例如有不足请指出.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JavaScript有多种继承方式,以下是几种常见的方式: 1. 原型链继承:通过将父类实例赋值给子类的原型来实现继承。 ``` function Parent() { this.name = 'Parent'; } Parent.prototype.sayName = function() { console.log(this.name); } function Child() {} Child.prototype = new Parent(); var child = new Child(); child.sayName(); // 输出 Parent ``` 2. 构造函数继承:通过在子类构造函数中调用父类构造函数来实现继承。 ``` function Parent() { this.name = 'Parent'; } Parent.prototype.sayName = function() { console.log(this.name); } function Child() { Parent.call(this); } var child = new Child(); child.sayName(); // 报错,因为子类没有继承父类的原型方法 ``` 3. 组合继承:结合原型链继承和构造函数继承方式。 ``` function Parent() { this.name = 'Parent'; } Parent.prototype.sayName = function() { console.log(this.name); } function Child() { Parent.call(this); } Child.prototype = new Parent(); Child.prototype.constructor = Child; var child = new Child(); child.sayName(); // 输出 Parent ``` 4. 寄生组合继承:对组合继承进行优化,避免了在子类原型上创建不必要的父类实例。 ``` function Parent() { this.name = 'Parent'; } Parent.prototype.sayName = function() { console.log(this.name); } function Child() { Parent.call(this); } Child.prototype = Object.create(Parent.prototype); Child.prototype.constructor = Child; var child = new Child(); child.sayName(); // 输出 Parent ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值