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);
		}




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章