国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 語(yǔ)言 > JavaScript > 正文

實(shí)例介紹JavaScript中多種組合繼承

2024-05-06 15:43:01
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

1. 組合繼承:又叫偽經(jīng)典繼承,是指將原型鏈和借用構(gòu)造函數(shù)技術(shù)組合在一塊的一種繼承方式。

下面來(lái)看一個(gè)例子:

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; }  //繼承方法 SubType.prototype = new SuperType(); SubType.prototype.sayAge = function() {  alert(this.age); }  var instance1 = new SubType("Nicholas", 29); instance1.colors.push("black"); alert(instance1.colors); //red,blue,green,black instance1.sayName(); //Nicholas instance1.sayAge(); //29  var instance2 = new SubType("Greg", 27); alert(instance2.colors); //red,blue,green instance2.sayName(); //Greg instance2.sayAge(); //27

組合繼承避免了原型鏈和借用構(gòu)造函數(shù)的缺陷,融合它們的優(yōu)點(diǎn)。

2. 原型式繼承

可以在不必預(yù)先定義構(gòu)造函數(shù)的情況下實(shí)現(xiàn)繼承,其本質(zhì)是執(zhí)行對(duì)給定對(duì)象的淺復(fù)制。而復(fù)制得到的副本還可以得到進(jìn)一步的改造。

function object(o) {  function F(){};  F.prototype = o;  return new F; }  var person = {  name: "Nicholas",  friends: ["Shelby", "Court", "Van"] };  var antherPerson = object(person); antherPerson.name = "Greg"; antherPerson.friends.push("Rob");  var antherPerson = object(person); antherPerson.name = "Linda"; antherPerson.friends.push("Barbie");  alert(person.friends); //Shelby,Court,Van,Rob,Barbie

3. 寄生式繼承

與原型式繼承非常相似,也是基于某個(gè)對(duì)象或某些信息創(chuàng)建一個(gè)對(duì)象,然后增強(qiáng)對(duì)象,最后返回對(duì)象。為了解決組合繼承模式由于多次調(diào)用超類型構(gòu)造函數(shù)而導(dǎo)致的低效率問(wèn)題,可以將這個(gè)模式與組合繼承一起使用。

function object(o) {  function F(){};  F.prototype = o;  return new F; } function createAnother(original) {  var clone = object(original);  clone.sayHi = function() {   alert("Hi");  };  return clone; }  var person = {  name: "Nicholas",  friends: ["Shelby", "Court", "Van"] };  var anotherPerson = createAnother(person); anotherPerson.sayHi();

4. 寄生組合式繼承

集寄生式繼承和組合繼承的優(yōu)點(diǎn)與一身,是實(shí)現(xiàn)基本類型繼承的最有效方式。

//繼承原型 function extend(subType, superType) {  function F(){};  F.prototype = superType.prototype;   var prototype = new F;  prototype.constructor = subType;  subType.prototype = prototype; }  //超類方法 function SuperType(name) {  this.name = name;  this.colors = ["red", "blue", "green"]; } SuperType.prototype.sayName = function() {  return this.name; }  //子類方法 function SubType(name, age) {  SuperType.call(this, name);  this.age = age; }  //繼承超類的原型 extend(SubType, SuperType);  //子類方法 SubType.prototype.sayAge = function() {  return this.age; }  var instance1 = new SubType("Shelby"); var instance2 = new SubType("Court", 28);  instance1.colors.push('black');  alert(instance1.colors); //red,blue,green,black alert(instance2.colors); //red,blue,green  alert(instance1 instanceof SubType); //true alert(instance1 instanceof SuperType); //true            
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 天镇县| 克拉玛依市| 漾濞| 乌兰察布市| 青冈县| 定州市| 石台县| 海南省| 武强县| 桐梓县| 明溪县| 东源县| 百色市| 景洪市| 海兴县| 江源县| 融水| 个旧市| 利川市| 梅河口市| 玉田县| 香格里拉县| 天柱县| 广饶县| 札达县| 德昌县| 长治县| 常熟市| 宁晋县| 泰来县| 正定县| 凌源市| 巴林左旗| 尚志市| 隆德县| 和平县| 秭归县| 汕头市| 和政县| 江达县| 白银市|