javascript的几种继承方法

1、原型链

原理:利用原型让一个引用类型继承另外一个引用类型的属性和方法。

构造函数,原型,实例之间的关系:每个构造函数都有一个原型对象,原型对象包含一个指向构造函数的指针,而实例都包含一个指向原型对象的内部指针。

原型链实现继承例子:

function SuperType() {
			this.property = true;
		}
		SuperType.prototype.getSuperValue = function() {
			return this.property;
		}
		function SubType() {
			this.property = false;
		}
		//继承了SuperType
		SubType.prototype = new SuperType();
		SubType.prototype.getSubValue = function (){
			return this.property;
		}
		var instance = new SubType();
		console.log(instance.getSuperValue());//true
2、借用构造函数

原理:在子类型构造函数的内部调用超类构造函数,通过使用call()和apply()方法可以在新创建的对象上执行构造函数。

function SuperType(){
			this.colors=["red","blue","green"];
		}
		function SubType(){
			SuperType.call(this);
		}
		var instance1=new SubType();
		instance1.colors.push("black");
		console.log(instance1.colors);
		var instance2=new SubType();
		console.log(instance2.colors);

3、组合继承

原理::将原型链和借用构造函数的技术组合在一块,从而发挥两者之长的一种继承模式。

 function SuperType(name){
		 	this.name=name;
		 	this.colors=["red","blue","green"];
		 }
		 SuperType.prototype.sayName=function(){
		 	console.log(this.name);
		 }
		 function SubType(name,age){
		 	SuperType.call(this,name);
		 	this.age=age;
		 }
		 //继承方法
		 SubType.prototype=new SuperType();
		 SubType.prototype.constructor=SubType;
		 SubType.prototype.sayAge=function(){
		 	console.log(this.age);
		 }
		 var instance1=new SubType("EvanChen",18);
		 instance1.colors.push("black");
		 console.log(instance1.colors);
		 instance1.sayName();
		 instance1.sayAge();
		 var instance2=new SubType("EvanChen666",20);
		 console.log(instance2.colors);
		 instance2.sayName();
		 instance2.sayAge();
4、原型链继承

原理:借助原型可以基于已有的对象创建新对象,同时还不必须因此创建自定义的类型。

var person={
		 	name:"EvanChen",
		 	friends:["Shelby","Court","Van"]
		 };
		 var anotherPerson=Object.create(person);
		 anotherPerson.name="Greg";
		 anotherPerson.friends.push("Rob");
		 var yetAnotherPerson=Object.create(person);
		 yetAnotherPerson.name="Linda";
		 yetAnotherPerson.friends.push("Barbie");
		 console.log(person.friends);

5、 寄生式继承

原理:创建一个仅用于封装继承过程的函数,该函数在内部以某种方式来增强对象,最后再像真正是它做了所有工作一样返回对象

 function createAnother(original){
		  	var clone=Object(original);
		  	clone.sayHi=function(){
		  		alert("hi");
		  	};
		  	return clone;
		  }
		  var person={
		  	name:"EvanChen",
		  	friends:["Shelby","Court","Van"]
		  };
		  var anotherPerson=createAnother(person);
		  anotherPerson.sayHi();

6、寄生组合式继承

原理:通过借用函数来继承属性,通过原型链的混成形式来继承方法

  function inheritProperty(subType, superType) {
			var prototype = object(superType.prototype);//创建对象
			prototype.constructor = subType;//增强对象
			subType.prototype = prototype;//指定对象
		  }
		  function SuperType(name){
			this.name = name;
			this.colors = ["red","blue","green"];
		  }
		SuperType.prototype.sayName = function (){
			alert(this.name);
		};
		function SubType(name,age){
			SuperType.call(this,name);
			this.age = age;
		}
			inheritProperty(SubType,SuperType);
		SubType.prototype.sayAge = function() {
			alert(this.age);
		}




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值